class UrlGenerator implements UrlGenerator

Traits

Constants

protected CACHED_SCHEME_CONTEXT_KEY

Context key for the cached URL scheme.

protected CACHED_ROOT_CONTEXT_KEY

Context key for the cached root URL.

protected FORCED_ROOT_CONTEXT_KEY

Context key for the forced root URL override.

protected FORCED_ASSET_ROOT_CONTEXT_KEY

Context key for the forced asset root URL override.

protected DEFAULT_PARAMETERS_CONTEXT_KEY

Context key for request-scoped default route parameters.

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected RouteCollectionInterface $routes

The route collection.

protected Request $request

The request instance.

protected string|null $assetRoot

The asset root URL.

protected string|null $forceScheme

The forced scheme for URLs.

protected string|null $rootNamespace

The root namespace being applied to controller actions.

protected null|callable $sessionResolver

The session resolver callable.

protected null|callable $keyResolver

The encryption key resolver callable.

protected null|callable $missingNamedRouteResolver

The missing named route resolver callable.

protected Closure|null $originResolver

The callback used to resolve the URL origin.

protected Closure|null $defaultsResolver

The callback used to resolve default route parameters.

protected Closure|null $formatHostUsing

The callback to use to format hosts.

protected Closure|null $formatPathUsing

The callback to use to format paths.

protected RouteUrlGenerator|null $routeGenerator

The route URL generator instance.

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(RouteCollectionInterface $routes, Request $request, string|null $assetRoot = null)

Create a new URL generator instance.

string
full()

Get the full URL for the current request.

string
current()

Get the current URL for the request.

string
previous(bool|string $fallback = false)

Get the URL for the previous request.

string
previousPath(bool|string $fallback = false)

Get the previous path info for the request.

string|null
getPreviousUrlFromSession()

Get the previous URL from the session if possible.

string
to(string $path, array|string $extra = [], bool|null $secure = null)

Generate an absolute URL to the given path.

string
query(string $path, array $query = [], array|string $extra = [], bool|null $secure = null)

Generate an absolute URL with the given query parameters.

string
secure(string $path, array $parameters = [])

Generate a secure, absolute URL to the given path.

string
asset(string $path, bool|null $secure = null)

Generate the URL to an application asset.

string
secureAsset(string $path)

Generate the URL to a secure asset.

string
assetFrom(string $root, string $path, bool|null $secure = null)

Generate the URL to an asset from a custom root domain such as CDN, etc.

string
removeIndex(string $root)

Remove the index.php file from a path.

string
formatScheme(bool|null $secure = null)

Get the default scheme for a raw URL.

string
signedRoute(BackedEnum|string $name, mixed $parameters = [], DateInterval|DateTimeInterface|int|null $expiration = null, bool $absolute = true)

Create a signed route URL for a named route.

void
ensureSignedRouteParametersAreNotReserved(array $parameters)

Ensure the given signed route parameters are not reserved.

string
temporarySignedRoute(BackedEnum|string $name, DateInterval|DateTimeInterface|int $expiration, array $parameters = [], bool $absolute = true)

Create a temporary signed route URL for a named route.

bool
hasValidSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])

Determine if the given request has a valid signature.

bool
hasValidRelativeSignature(Request $request, Closure|array $ignoreQuery = [])

Determine if the given request has a valid signature for a relative URL.

bool
hasCorrectSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])

Determine if the signature from the given request matches the URL.

bool
signatureHasNotExpired(Request $request)

Determine if the expires timestamp from the given request is not from the past.

string
route(BackedEnum|string $name, mixed $parameters = [], bool $absolute = true)

Get the URL to a named route.

string
toRoute(Route $route, mixed $parameters, bool $absolute)

Get the URL for a given route instance.

string
action(array|string $action, array|string $parameters = [], bool $absolute = true)

Get the URL to a controller action.

string
formatAction(array|string $action)

Format the given controller action.

array
formatParameters(mixed $parameters)

Format the array of URL parameters.

array
extractQueryString(string $path)

Extract the query string from the given path.

string
formatRoot(string $scheme, string|null $root = null)

Get the base URL for the request.

string
resolveRoot(bool $useOriginResolver = true)

Resolve the root URL for the current coroutine.

string
format(string $root, string $path, Route|null $route = null)

Format the given URL segments into a single URL.

bool
isValidUrl(string $path)

Determine if the given path is a valid URL.

routeUrl()

Get the route URL generator instance.

void
resolveDefaultsUsing(Closure|null $resolver)

Register the default route parameter resolver.

void
defaults(array $defaults)

Set the default named parameters used by the URL generator.

array
getDefaultParameters()

Get the effective default named parameters used by the URL generator.

void
useDefaults(array|null $defaults)

Replace the default route parameters for the current coroutine.

void
forceScheme(string|null $scheme)

Force the scheme for URLs.

string|null
getForcedScheme()

Get the forced scheme for URLs.

void
forceHttps(bool $force = true)

Force the use of the HTTPS scheme for all generated URLs.

void
resolveOriginUsing(Closure|null $resolver)

Register the URL origin resolver.

void
useOrigin(string|null $root)

Set the URL origin for the current coroutine.

void
useAssetOrigin(string|null $root)

Set the asset URL origin for the current coroutine.

formatHostUsing(Closure $callback)

Set a callback to be used to format the host of generated URLs.

formatPathUsing(Closure $callback)

Set a callback to be used to format the path of generated URLs.

pathFormatter()

Get the path formatter being used by the URL generator.

getRequest()

Get the request instance.

void
setRequest(Request $request)

Set the current request instance.

setRoutes(RouteCollectionInterface $routes)

Set the route collection.

mixed
getSession()

Get the session implementation from the resolver.

setSessionResolver(callable $sessionResolver)

Set the session resolver for the generator.

setKeyResolver(callable $keyResolver)

Set the encryption key resolver.

withKeyResolver(callable $keyResolver)

Clone a new instance of the URL generator with a different encryption key resolver.

resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)

Set the callback that should be used to attempt to resolve missing named routes.

string|null
getRootControllerNamespace()

Get the root controller namespace.

setRootControllerNamespace(string $rootNamespace)

Set the root controller namespace.

static void
flushState()

Flush all static state.

Details

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 41
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 57
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 65
protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 129
__construct(RouteCollectionInterface $routes, Request $request, string|null $assetRoot = null)

Create a new URL generator instance.

Parameters

RouteCollectionInterface $routes
Request $request
string|null $assetRoot

at line 140
string full()

Get the full URL for the current request.

Return Value

string

at line 148
string current()

Get the current URL for the request.

Return Value

string

at line 156
string previous(bool|string $fallback = false)

Get the URL for the previous request.

Parameters

bool|string $fallback

Return Value

string

at line 175
string previousPath(bool|string $fallback = false)

Get the previous path info for the request.

Parameters

bool|string $fallback

Return Value

string

at line 185
protected string|null getPreviousUrlFromSession()

Get the previous URL from the session if possible.

Return Value

string|null

at line 193
string to(string $path, array|string $extra = [], bool|null $secure = null)

Generate an absolute URL to the given path.

Parameters

string $path
array|string $extra
bool|null $secure

Return Value

string

at line 226
string query(string $path, array $query = [], array|string $extra = [], bool|null $secure = null)

Generate an absolute URL with the given query parameters.

Parameters

string $path
array $query
array|string $extra
bool|null $secure

Return Value

string

at line 240
string secure(string $path, array $parameters = [])

Generate a secure, absolute URL to the given path.

Parameters

string $path
array $parameters

Return Value

string

at line 248
string asset(string $path, bool|null $secure = null)

Generate the URL to an application asset.

Parameters

string $path
bool|null $secure

Return Value

string

at line 271
string secureAsset(string $path)

Generate the URL to a secure asset.

Parameters

string $path

Return Value

string

at line 279
string assetFrom(string $root, string $path, bool|null $secure = null)

Generate the URL to an asset from a custom root domain such as CDN, etc.

Parameters

string $root
string $path
bool|null $secure

Return Value

string

at line 292
protected string removeIndex(string $root)

Remove the index.php file from a path.

Parameters

string $root

Return Value

string

at line 302
string formatScheme(bool|null $secure = null)

Get the default scheme for a raw URL.

Parameters

bool|null $secure

Return Value

string

at line 318
string signedRoute(BackedEnum|string $name, mixed $parameters = [], DateInterval|DateTimeInterface|int|null $expiration = null, bool $absolute = true)

Create a signed route URL for a named route.

Parameters

BackedEnum|string $name
mixed $parameters
DateInterval|DateTimeInterface|int|null $expiration
bool $absolute

Return Value

string

Exceptions

InvalidArgumentException

at line 346
protected void ensureSignedRouteParametersAreNotReserved(array $parameters)

Ensure the given signed route parameters are not reserved.

Parameters

array $parameters

Return Value

void

Exceptions

InvalidArgumentException

at line 364
string temporarySignedRoute(BackedEnum|string $name, DateInterval|DateTimeInterface|int $expiration, array $parameters = [], bool $absolute = true)

Create a temporary signed route URL for a named route.

Parameters

BackedEnum|string $name
DateInterval|DateTimeInterface|int $expiration
array $parameters
bool $absolute

Return Value

string

at line 372
bool hasValidSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])

Determine if the given request has a valid signature.

Parameters

Request $request
bool $absolute
Closure|array $ignoreQuery

Return Value

bool

at line 381
bool hasValidRelativeSignature(Request $request, Closure|array $ignoreQuery = [])

Determine if the given request has a valid signature for a relative URL.

Parameters

Request $request
Closure|array $ignoreQuery

Return Value

bool

at line 389
bool hasCorrectSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])

Determine if the signature from the given request matches the URL.

Parameters

Request $request
bool $absolute
Closure|array $ignoreQuery

Return Value

bool

at line 430
bool signatureHasNotExpired(Request $request)

Determine if the expires timestamp from the given request is not from the past.

Parameters

Request $request

Return Value

bool

at line 443
string route(BackedEnum|string $name, mixed $parameters = [], bool $absolute = true)

Get the URL to a named route.

Parameters

BackedEnum|string $name
mixed $parameters
bool $absolute

Return Value

string

Exceptions

RouteNotFoundException
InvalidArgumentException

at line 466
string toRoute(Route $route, mixed $parameters, bool $absolute)

Get the URL for a given route instance.

Parameters

Route $route
mixed $parameters
bool $absolute

Return Value

string

Exceptions

UrlGenerationException

at line 480
string action(array|string $action, array|string $parameters = [], bool $absolute = true)

Get the URL to a controller action.

Parameters

array|string $action
array|string $parameters
bool $absolute

Return Value

string

Exceptions

InvalidArgumentException

at line 492
protected string formatAction(array|string $action)

Format the given controller action.

Parameters

array|string $action

Return Value

string

at line 508
array formatParameters(mixed $parameters)

Format the array of URL parameters.

Parameters

mixed $parameters

Return Value

array

at line 524
protected array extractQueryString(string $path)

Extract the query string from the given path.

Parameters

string $path

Return Value

array

at line 539
string formatRoot(string $scheme, string|null $root = null)

Get the base URL for the request.

Parameters

string $scheme
string|null $root

Return Value

string

at line 553
protected string resolveRoot(bool $useOriginResolver = true)

Resolve the root URL for the current coroutine.

Parameters

bool $useOriginResolver

Return Value

string

at line 571
string format(string $root, string $path, Route|null $route = null)

Format the given URL segments into a single URL.

Parameters

string $root
string $path
Route|null $route

Return Value

string

at line 589
bool isValidUrl(string $path)

Determine if the given path is a valid URL.

Parameters

string $path

Return Value

bool

at line 605
protected RouteUrlGenerator routeUrl()

Get the route URL generator instance.

The RouteUrlGenerator is cached on the instance for its default parameters, but its request is always resolved fresh from the UrlGenerator to ensure coroutine safety in Swoole's long-lived workers.

Return Value

RouteUrlGenerator

at line 620
void resolveDefaultsUsing(Closure|null $resolver)

Register the default route parameter resolver.

Boot-only. The callback persists on the worker-shared URL generator and is evaluated for subsequent route generations across all coroutines.

Parameters

Closure|null $resolver

Return Value

void

at line 636
void defaults(array $defaults)

Set the default named parameters used by the URL generator.

Request-scoped when a request is present: the defaults are stored in coroutine Context and isolated to the current request. Outside a request (boot, queue jobs, scheduled tasks, console), they are stored on the worker-global route URL generator, shared across concurrent coroutines and persisting for the worker lifetime until changed or flushed.

Use useDefaults() for operation-local defaults outside an HTTP request.

Parameters

array $defaults

Return Value

void

at line 653
array getDefaultParameters()

Get the effective default named parameters used by the URL generator.

Return Value

array

at line 671
void useDefaults(array|null $defaults)

Replace the default route parameters for the current coroutine.

Parameters

array|null $defaults

Return Value

void

at line 689
void forceScheme(string|null $scheme)

Force the scheme for URLs.

Boot-only. The scheme is stored on the worker-shared URL generator singleton and affects subsequent requests across coroutines; not safe to call per-request.

Parameters

string|null $scheme

Return Value

void

at line 699
string|null getForcedScheme()

Get the forced scheme for URLs.

Return Value

string|null

at line 711
void forceHttps(bool $force = true)

Force the use of the HTTPS scheme for all generated URLs.

Boot-only. The scheme is stored on the worker-shared URL generator singleton and affects subsequent requests across coroutines; not safe to call per-request.

Parameters

bool $force

Return Value

void

at line 722
void resolveOriginUsing(Closure|null $resolver)

Register the URL origin resolver.

Boot-only. The callback persists on the worker-shared URL generator and is evaluated for subsequent URL generations across all coroutines.

Parameters

Closure|null $resolver

Return Value

void

at line 733
void useOrigin(string|null $root)

Set the URL origin for the current coroutine.

Stored in coroutine Context so one request, job, or command cannot affect concurrent work.

Parameters

string|null $root

Return Value

void

at line 751
void useAssetOrigin(string|null $root)

Set the asset URL origin for the current coroutine.

Stored in coroutine Context so one request, job, or command cannot affect concurrent work.

Parameters

string|null $root

Return Value

void

at line 765
UrlGenerator formatHostUsing(Closure $callback)

Set a callback to be used to format the host of generated URLs.

Parameters

Closure $callback

Return Value

UrlGenerator

at line 777
UrlGenerator formatPathUsing(Closure $callback)

Set a callback to be used to format the path of generated URLs.

Parameters

Closure $callback

Return Value

UrlGenerator

at line 787
Closure pathFormatter()

Get the path formatter being used by the URL generator.

Return Value

Closure

at line 801
Request getRequest()

Get the request instance.

In Swoole's long-lived workers, the UrlGenerator is a singleton. The request must be resolved from the coroutine-local RequestContext to avoid one coroutine's request leaking to another.

Return Value

Request

at line 814
void setRequest(Request $request)

Set the current request instance.

Tests only. Per-request code should rely on RequestContext (read first by getRequest()); this setter writes the singleton UrlGenerator's fallback request used outside coroutine contexts, plus mutates the shared routeGenerator — runtime use races across coroutines.

Parameters

Request $request

Return Value

void

at line 835
UrlGenerator setRoutes(RouteCollectionInterface $routes)

Set the route collection.

Parameters

RouteCollectionInterface $routes

Return Value

UrlGenerator

at line 845
protected mixed getSession()

Get the session implementation from the resolver.

Return Value

mixed

at line 859
UrlGenerator setSessionResolver(callable $sessionResolver)

Set the session resolver for the generator.

Parameters

callable $sessionResolver

Return Value

UrlGenerator

at line 871
UrlGenerator setKeyResolver(callable $keyResolver)

Set the encryption key resolver.

Parameters

callable $keyResolver

Return Value

UrlGenerator

at line 881
UrlGenerator withKeyResolver(callable $keyResolver)

Clone a new instance of the URL generator with a different encryption key resolver.

Parameters

callable $keyResolver

Return Value

UrlGenerator

at line 891
UrlGenerator resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)

Set the callback that should be used to attempt to resolve missing named routes.

Parameters

callable $missingNamedRouteResolver

Return Value

UrlGenerator

at line 901
string|null getRootControllerNamespace()

Get the root controller namespace.

Return Value

string|null

at line 911
UrlGenerator setRootControllerNamespace(string $rootNamespace)

Set the root controller namespace.

Parameters

string $rootNamespace

Return Value

UrlGenerator

at line 921
static void flushState()

Flush all static state.

Return Value

void