class UrlGenerator implements UrlGenerator (View source)

Traits

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 $assetRoot

The asset root URL.

protected string $forcedRoot

The forced URL root.

protected string $forceScheme

The forced scheme for URLs.

protected string|null $cachedRoot

A cached copy of the URL root for the current request.

protected string|null $cachedScheme

A cached copy of the URL scheme for the current request.

protected string $rootNamespace

The root namespace being applied to controller actions.

protected callable $sessionResolver

The session resolver callable.

protected callable $keyResolver

The encryption key resolver callable.

protected callable $missingNamedRouteResolver

The missing named route resolver callable.

protected Closure $formatHostUsing

The callback to use to format hosts.

protected Closure $formatPathUsing

The callback to use to format paths.

protected RouteUrlGenerator|null $routeGenerator

The route URL generator instance.

Methods

int
secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

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

Get the "available at" UNIX timestamp.

parseDateInterval(DateTimeInterface|DateInterval|int $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 $endTime = null)

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

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

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

Checks 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(mixed $fallback = false)

Get the URL for the previous request.

string
previousPath(mixed $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, mixed $extra = [], bool|null $secure = null)

Generate an absolute URL to the given path.

string
query(string $path, array $query = [], mixed $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(string $name, mixed $parameters = [], DateTimeInterface|DateInterval|int|null $expiration = null, bool $absolute = true)

Create a signed route URL for a named route.

void
ensureSignedRouteParametersAreNotReserved(mixed $parameters)

Ensure the given signed route parameters are not reserved.

string
temporarySignedRoute(string $name, DateTimeInterface|DateInterval|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(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(string|array $action, mixed $parameters = [], bool $absolute = true)

Get the URL to a controller action.

string
formatAction(string|array $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
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
defaults(array $defaults)

Set the default named parameters used by the URL generator.

array
getDefaultParameters()

Get the default named parameters used by the URL generator.

void
forceScheme(string|null $scheme)

Force the scheme for URLs.

void
forceHttps(bool $force = true)

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

void
useOrigin(string|null $root)

Set the URL origin for all generated URLs.

void
forceRootUrl(string|null $root) deprecated

Set the forced root URL.

void
useAssetOrigin(string|null $root)

Set the URL origin for all generated asset URLs.

$this
formatHostUsing(Closure $callback)

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

$this
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.

$this
setRoutes(RouteCollectionInterface $routes)

Set the route collection.

Store|null
getSession()

Get the session implementation from the resolver.

$this
setSessionResolver(callable $sessionResolver)

Set the session resolver for the generator.

$this
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.

$this
resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)

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

string
getRootControllerNamespace()

Get the root controller namespace.

$this
setRootControllerNamespace(string $rootNamespace)

Set the root controller namespace.

Details

protected int secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

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

Get the "available at" UNIX timestamp.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

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

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

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

DateTimeInterface|int

protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

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

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

Parameters

float $startTime
float $endTime

Return Value

string

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

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

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

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

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

Create a new URL Generator instance.

Parameters

RouteCollectionInterface $routes
Request $request
string|null $assetRoot

string full()

Get the full URL for the current request.

Return Value

string

string current()

Get the current URL for the request.

Return Value

string

string previous(mixed $fallback = false)

Get the URL for the previous request.

Parameters

mixed $fallback

Return Value

string

string previousPath(mixed $fallback = false)

Get the previous path info for the request.

Parameters

mixed $fallback

Return Value

string

protected string|null getPreviousUrlFromSession()

Get the previous URL from the session if possible.

Return Value

string|null

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

Generate an absolute URL to the given path.

Parameters

string $path
mixed $extra
bool|null $secure

Return Value

string

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

Generate an absolute URL with the given query parameters.

Parameters

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

Return Value

string

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

Generate a secure, absolute URL to the given path.

Parameters

string $path
array $parameters

Return Value

string

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

Generate the URL to an application asset.

Parameters

string $path
bool|null $secure

Return Value

string

string secureAsset(string $path)

Generate the URL to a secure asset.

Parameters

string $path

Return Value

string

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

protected string removeIndex(string $root)

Remove the index.php file from a path.

Parameters

string $root

Return Value

string

string formatScheme(bool|null $secure = null)

Get the default scheme for a raw URL.

Parameters

bool|null $secure

Return Value

string

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

Create a signed route URL for a named route.

Parameters

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

Return Value

string

Exceptions

InvalidArgumentException

protected void ensureSignedRouteParametersAreNotReserved(mixed $parameters)

Ensure the given signed route parameters are not reserved.

Parameters

mixed $parameters

Return Value

void

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

Create a temporary signed route URL for a named route.

Parameters

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

Return Value

string

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

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

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

bool signatureHasNotExpired(Request $request)

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

Parameters

Request $request

Return Value

bool

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

Get the URL to a named route.

Parameters

string $name
mixed $parameters
bool $absolute

Return Value

string

Exceptions

InvalidArgumentException

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

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

Get the URL to a controller action.

Parameters

string|array $action
mixed $parameters
bool $absolute

Return Value

string

Exceptions

InvalidArgumentException

protected string formatAction(string|array $action)

Format the given controller action.

Parameters

string|array $action

Return Value

string

array formatParameters(mixed $parameters)

Format the array of URL parameters.

Parameters

mixed $parameters

Return Value

array

protected array extractQueryString(string $path)

Extract the query string from the given path.

Parameters

string $path

Return Value

array

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

Get the base URL for the request.

Parameters

string $scheme
string|null $root

Return Value

string

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

bool isValidUrl(string $path)

Determine if the given path is a valid URL.

Parameters

string $path

Return Value

bool

protected RouteUrlGenerator routeUrl()

Get the Route URL generator instance.

Return Value

RouteUrlGenerator

void defaults(array $defaults)

Set the default named parameters used by the URL generator.

Parameters

array $defaults

Return Value

void

array getDefaultParameters()

Get the default named parameters used by the URL generator.

Return Value

array

void forceScheme(string|null $scheme)

Force the scheme for URLs.

Parameters

string|null $scheme

Return Value

void

void forceHttps(bool $force = true)

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

Parameters

bool $force

Return Value

void

void useOrigin(string|null $root)

Set the URL origin for all generated URLs.

Parameters

string|null $root

Return Value

void

void forceRootUrl(string|null $root) deprecated

deprecated Use useOrigin

Set the forced root URL.

Parameters

string|null $root

Return Value

void

void useAssetOrigin(string|null $root)

Set the URL origin for all generated asset URLs.

Parameters

string|null $root

Return Value

void

$this formatHostUsing(Closure $callback)

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

Parameters

Closure $callback

Return Value

$this

$this formatPathUsing(Closure $callback)

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

Parameters

Closure $callback

Return Value

$this

Closure pathFormatter()

Get the path formatter being used by the URL generator.

Return Value

Closure

Request getRequest()

Get the request instance.

Return Value

Request

void setRequest(Request $request)

Set the current request instance.

Parameters

Request $request

Return Value

void

$this setRoutes(RouteCollectionInterface $routes)

Set the route collection.

Parameters

RouteCollectionInterface $routes

Return Value

$this

protected Store|null getSession()

Get the session implementation from the resolver.

Return Value

Store|null

$this setSessionResolver(callable $sessionResolver)

Set the session resolver for the generator.

Parameters

callable $sessionResolver

Return Value

$this

$this setKeyResolver(callable $keyResolver)

Set the encryption key resolver.

Parameters

callable $keyResolver

Return Value

$this

UrlGenerator withKeyResolver(callable $keyResolver)

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

Parameters

callable $keyResolver

Return Value

UrlGenerator

$this resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)

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

Parameters

callable $missingNamedRouteResolver

Return Value

$this

string getRootControllerNamespace()

Get the root controller namespace.

Return Value

string

$this setRootControllerNamespace(string $rootNamespace)

Set the root controller namespace.

Parameters

string $rootNamespace

Return Value

$this