UrlGenerator
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
Get the number of seconds until the given DateTime.
Get the "available at" UNIX timestamp.
If the given value is an interval, convert it to a DateTime instance.
Given a start time, format the total run time for human readability.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Create a new URL generator instance.
Get the full URL for the current request.
Get the current URL for the request.
Get the URL for the previous request.
Get the previous path info for the request.
Get the previous URL from the session if possible.
Generate an absolute URL to the given path.
Generate an absolute URL with the given query parameters.
Generate a secure, absolute URL to the given path.
Generate the URL to an application asset.
Generate the URL to a secure asset.
Generate the URL to an asset from a custom root domain such as CDN, etc.
Remove the index.php file from a path.
Get the default scheme for a raw URL.
Create a signed route URL for a named route.
Ensure the given signed route parameters are not reserved.
Create a temporary signed route URL for a named route.
Determine if the given request has a valid signature.
Determine if the given request has a valid signature for a relative URL.
Determine if the signature from the given request matches the URL.
Determine if the expires timestamp from the given request is not from the past.
Get the URL to a named route.
Get the URL to a controller action.
Format the given controller action.
Format the array of URL parameters.
Extract the query string from the given path.
Get the base URL for the request.
Resolve the root URL for the current coroutine.
Determine if the given path is a valid URL.
Get the route URL generator instance.
Set the default named parameters used by the URL generator.
Get the effective default named parameters used by the URL generator.
Replace the default route parameters for the current coroutine.
Force the scheme for URLs.
Get the forced scheme for URLs.
Force the use of the HTTPS scheme for all generated URLs.
Set the URL origin for the current coroutine.
Set the asset URL origin for the current coroutine.
Get the path formatter being used by the URL generator.
Get the request instance.
Get the session implementation from the resolver.
Set the session resolver for the generator.
Set the encryption key resolver.
Clone a new instance of the URL generator with a different encryption key resolver.
Set the callback that should be used to attempt to resolve missing named routes.
Get the root controller namespace.
Set the root controller namespace.
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.
in
InteractsWithTime at line 29
protected int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)
Get the "available at" UNIX timestamp.
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.
in
InteractsWithTime at line 57
protected int
currentTime()
Get the current system time as a UNIX timestamp.
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.
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.
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.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
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.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 103
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
at line 129
__construct(RouteCollectionInterface $routes, Request $request, string|null $assetRoot = null)
Create a new URL generator instance.
at line 140
string
full()
Get the full URL for the current request.
at line 148
string
current()
Get the current URL for the request.
at line 156
string
previous(bool|string $fallback = false)
Get the URL for the previous request.
at line 175
string
previousPath(bool|string $fallback = false)
Get the previous path info for the request.
at line 185
protected string|null
getPreviousUrlFromSession()
Get the previous URL from the session if possible.
at line 193
string
to(string $path, array|string $extra = [], bool|null $secure = null)
Generate an absolute URL to the given path.
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.
at line 240
string
secure(string $path, array $parameters = [])
Generate a secure, absolute URL to the given path.
at line 248
string
asset(string $path, bool|null $secure = null)
Generate the URL to an application asset.
at line 271
string
secureAsset(string $path)
Generate the URL to a secure asset.
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.
at line 292
protected string
removeIndex(string $root)
Remove the index.php file from a path.
at line 302
string
formatScheme(bool|null $secure = null)
Get the default scheme for a raw URL.
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.
at line 346
protected void
ensureSignedRouteParametersAreNotReserved(array $parameters)
Ensure the given signed route parameters are not reserved.
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.
at line 372
bool
hasValidSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])
Determine if the given request has a valid signature.
at line 381
bool
hasValidRelativeSignature(Request $request, Closure|array $ignoreQuery = [])
Determine if the given request has a valid signature for a relative URL.
at line 389
bool
hasCorrectSignature(Request $request, bool $absolute = true, Closure|array $ignoreQuery = [])
Determine if the signature from the given request matches the URL.
at line 430
bool
signatureHasNotExpired(Request $request)
Determine if the expires timestamp from the given request is not from the past.
at line 443
string
route(BackedEnum|string $name, mixed $parameters = [], bool $absolute = true)
Get the URL to a named route.
at line 466
string
toRoute(Route $route, mixed $parameters, bool $absolute)
Get the URL for a given route instance.
at line 480
string
action(array|string $action, array|string $parameters = [], bool $absolute = true)
Get the URL to a controller action.
at line 492
protected string
formatAction(array|string $action)
Format the given controller action.
at line 508
array
formatParameters(mixed $parameters)
Format the array of URL parameters.
at line 524
protected array
extractQueryString(string $path)
Extract the query string from the given path.
at line 539
string
formatRoot(string $scheme, string|null $root = null)
Get the base URL for the request.
at line 553
protected string
resolveRoot(bool $useOriginResolver = true)
Resolve the root URL for the current coroutine.
at line 571
string
format(string $root, string $path, Route|null $route = null)
Format the given URL segments into a single URL.
at line 589
bool
isValidUrl(string $path)
Determine if the given path is a valid URL.
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.
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.
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.
at line 653
array
getDefaultParameters()
Get the effective default named parameters used by the URL generator.
at line 671
void
useDefaults(array|null $defaults)
Replace the default route parameters for the current coroutine.
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.
at line 699
string|null
getForcedScheme()
Get the forced scheme for URLs.
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.
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.
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.
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.
at line 765
UrlGenerator
formatHostUsing(Closure $callback)
Set a callback to be used to format the host of generated URLs.
at line 777
UrlGenerator
formatPathUsing(Closure $callback)
Set a callback to be used to format the path of generated URLs.
at line 787
Closure
pathFormatter()
Get the path formatter being used by the URL generator.
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.
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.
at line 835
UrlGenerator
setRoutes(RouteCollectionInterface $routes)
Set the route collection.
at line 845
protected mixed
getSession()
Get the session implementation from the resolver.
at line 859
UrlGenerator
setSessionResolver(callable $sessionResolver)
Set the session resolver for the generator.
at line 871
UrlGenerator
setKeyResolver(callable $keyResolver)
Set the encryption key resolver.
at line 881
UrlGenerator
withKeyResolver(callable $keyResolver)
Clone a new instance of the URL generator with a different encryption key resolver.
at line 891
UrlGenerator
resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)
Set the callback that should be used to attempt to resolve missing named routes.
at line 901
string|null
getRootControllerNamespace()
Get the root controller namespace.
at line 911
UrlGenerator
setRootControllerNamespace(string $rootNamespace)
Set the root controller namespace.
at line 921
static void
flushState()
Flush all static state.