class Router implements BindingRegistrar, Registrar mixin RouteRegistrar (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected Dispatcher $events

The event dispatcher instance.

protected Container $container

The IoC container instance.

protected RouteCollectionInterface $routes

The route collection instance.

protected Route|null $current

The currently dispatched route instance.

protected Request $currentRequest

The request currently being dispatched.

protected array $middleware

All of the short-hand keys for middlewares.

protected array $middlewareGroups

All of the middleware groups.

array $middlewarePriority

The priority-sorted list of middleware.

protected array $binders

The registered route value binders.

protected array $patterns

The globally available parameter patterns.

protected array $groupStack

The route group attribute stack.

protected array $implicitBindingCallback

The registered custom implicit binding callback.

static string[] $verbs

All of the verbs supported by the router.

Methods

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 into the router instance.

void
__construct(Dispatcher $events, Container|null $container = null)

Create a new Router instance.

get(string $uri, array|string|callable $action = null)

Register a new GET route with the router.

post(string $uri, array|string|callable $action = null)

Register a new POST route with the router.

put(string $uri, array|string|callable $action = null)

Register a new PUT route with the router.

patch(string $uri, array|string|callable $action = null)

Register a new PATCH route with the router.

delete(string $uri, array|string|callable $action = null)

Register a new DELETE route with the router.

options(string $uri, array|string|callable $action = null)

Register a new OPTIONS route with the router.

any(string $uri, array|string|callable|null $action = null)

Register a new route responding to all verbs.

fallback(array|string|callable|null $action)

Register a new fallback route with the router.

redirect(string $uri, string $destination, int $status = 302)

Create a redirect from one URI to another.

permanentRedirect(string $uri, string $destination)

Create a permanent redirect from one URI to another.

view(string $uri, string $view, array $data = [], int|array $status = 200, array $headers = [])

Register a new route that returns a view.

match(array|string $methods, string $uri, array|string|callable $action = null)

Register a new route with the given verbs.

void
resources(array $resources, array $options = [])

Register an array of resource controllers.

resource(string $name, string $controller, array $options = [])

Route a resource to a controller.

void
apiResources(array $resources, array $options = [])

Register an array of API resource controllers.

apiResource(string $name, string $controller, array $options = [])

Route an API resource to a controller.

void
singletons(array $singletons, array $options = [])

Register an array of singleton resource controllers.

singleton(string $name, string $controller, array $options = [])

Route a singleton resource to a controller.

void
apiSingletons(array $singletons, array $options = [])

Register an array of API singleton resource controllers.

apiSingleton(string $name, string $controller, array $options = [])

Route an API singleton resource to a controller.

void
group(array $attributes, Closure|string $routes)

Create a route group with shared attributes.

void
updateGroupStack(array $attributes)

Update the group stack with the given attributes.

array
mergeWithLastGroup(array $new, bool $prependExistingPrefix = true)

Merge the given array with the last group stack.

void
loadRoutes(Closure|string $routes)

Load the provided routes.

string
getLastGroupPrefix()

Get the prefix from the last group on the stack.

addRoute(array|string $methods, string $uri, array|string|callable|null $action)

Add a route to the underlying route collection.

createRoute(array|string $methods, string $uri, mixed $action)

Create a new route instance.

bool
actionReferencesController(mixed $action)

Determine if the action is routing to a controller.

array
convertToControllerAction(array|string $action)

Add a controller based route action to the action array.

string
prependGroupNamespace(string $class)

Prepend the last group namespace onto the use clause.

string
prependGroupController(string $class)

Prepend the last group controller onto the use clause.

newRoute(array|string $methods, string $uri, mixed $action)

Create a new Route object.

string
prefix(string $uri)

Prefix the given URI with the last prefix.

addWhereClausesToRoute(Route $route)

Add the necessary where clauses to the route based on its initial registration.

void
mergeGroupAttributesIntoRoute(Route $route)

Merge the group stack with the controller action.

Response
respondWithRoute(string $name)

Return the response returned by the given route.

Response
dispatch(Request $request)

Dispatch the request to the application.

Response
dispatchToRoute(Request $request)

Dispatch the request to a route and return the response.

findRoute(Request $request)

Find the route matching a given request.

Response
runRoute(Request $request, Route $route)

Return the response for the given route.

mixed
runRouteWithinStack(Route $route, Request $request)

Run the given route within a Stack "onion" instance.

array
gatherRouteMiddleware(Route $route)

Gather the middleware for the given route with resolved class names.

array
resolveMiddleware(array $middleware, array $excluded = [])

Resolve a flat array of middleware classes from the provided array.

array
sortMiddleware(Collection $middlewares)

Sort the given middleware by priority.

Response
prepareResponse(Request $request, mixed $response)

Create a response instance from the given value.

static Response
toResponse(Request $request, mixed $response)

Static version of prepareResponse.

substituteBindings(Route $route)

Substitute the route bindings onto the route.

void
substituteImplicitBindings(Route $route)

Substitute the implicit route bindings for the given route.

$this
substituteImplicitBindingsUsing(callable $callback)

Register a callback to to run after implicit bindings are substituted.

mixed
performBinding(string $key, string $value, Route $route)

Call the binding callback for the given key.

void
matched(string|callable $callback)

Register a route matched event listener.

array
getMiddleware()

Get all of the defined middleware short-hand names.

$this
aliasMiddleware(string $name, string $class)

Register a short-hand name for a middleware.

bool
hasMiddlewareGroup(string $name)

Check if a middlewareGroup with the given name exists.

array
getMiddlewareGroups()

Get all of the defined middleware groups.

$this
middlewareGroup(string $name, array $middleware)

Register a group of middleware.

$this
prependMiddlewareToGroup(string $group, string $middleware)

Add a middleware to the beginning of a middleware group.

$this
pushMiddlewareToGroup(string $group, string $middleware)

Add a middleware to the end of a middleware group.

$this
removeMiddlewareFromGroup(string $group, string $middleware)

Remove the given middleware from the specified group.

$this
flushMiddlewareGroups()

Flush the router's middleware groups.

void
bind(string $key, string|callable $binder)

Add a new route parameter binder.

void
model(string $key, string $class, Closure|null $callback = null)

Register a model binder for a wildcard.

getBindingCallback(string $key)

Get the binding callback for a given binding.

array
getPatterns()

Get the global "where" patterns.

void
pattern(string $key, string $pattern)

Set a global where pattern on all routes.

void
patterns(array $patterns)

Set a group of global where patterns on all routes.

bool
hasGroupStack()

Determine if the router currently has a group stack.

array
getGroupStack()

Get the current group stack for the router.

mixed
input(string $key, string|null $default = null)

Get a route parameter for the current route.

getCurrentRequest()

Get the request currently being dispatched.

Route|null
getCurrentRoute()

Get the currently dispatched route instance.

Route|null
current()

Get the currently dispatched route instance.

bool
has(string|array $name)

Check if a route with the given name exists.

string|null
currentRouteName()

Get the current route name.

bool
is(mixed ...$patterns)

Alias for the "currentRouteNamed" method.

bool
currentRouteNamed(mixed ...$patterns)

Determine if the current route matches a pattern.

string|null
currentRouteAction()

Get the current route action.

bool
uses(array ...$patterns)

Alias for the "currentRouteUses" method.

bool
currentRouteUses(string $action)

Determine if the current route action matches a given action.

void
singularResourceParameters(bool $singular = true)

Set the unmapped global resource parameters to singular.

void
resourceParameters(array $parameters = [])

Set the global resource parameter mapping.

array|null
resourceVerbs(array $verbs = [])

Get or set the verbs used in the resource URIs.

getRoutes()

Get the underlying route collection.

void
setRoutes(RouteCollection $routes)

Set the route collection instance.

void
setCompiledRoutes(array $routes)

Set the compiled route collection instance.

static array
uniqueMiddleware(array $middleware)

Remove any duplicate middleware from the given array.

$this
setContainer(Container $container)

Set the container instance used by the router.

Details

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 into the router instance.

Parameters

string $method
array $parameters

Return Value

mixed

void __construct(Dispatcher $events, Container|null $container = null)

Create a new Router instance.

Parameters

Dispatcher $events
Container|null $container

Return Value

void

Route get(string $uri, array|string|callable $action = null)

Register a new GET route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route post(string $uri, array|string|callable $action = null)

Register a new POST route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route put(string $uri, array|string|callable $action = null)

Register a new PUT route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route patch(string $uri, array|string|callable $action = null)

Register a new PATCH route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route delete(string $uri, array|string|callable $action = null)

Register a new DELETE route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route options(string $uri, array|string|callable $action = null)

Register a new OPTIONS route with the router.

Parameters

string $uri
array|string|callable $action

Return Value

Route

Route any(string $uri, array|string|callable|null $action = null)

Register a new route responding to all verbs.

Parameters

string $uri
array|string|callable|null $action

Return Value

Route

Route fallback(array|string|callable|null $action)

Register a new fallback route with the router.

Parameters

array|string|callable|null $action

Return Value

Route

Route redirect(string $uri, string $destination, int $status = 302)

Create a redirect from one URI to another.

Parameters

string $uri
string $destination
int $status

Return Value

Route

Route permanentRedirect(string $uri, string $destination)

Create a permanent redirect from one URI to another.

Parameters

string $uri
string $destination

Return Value

Route

Route view(string $uri, string $view, array $data = [], int|array $status = 200, array $headers = [])

Register a new route that returns a view.

Parameters

string $uri
string $view
array $data
int|array $status
array $headers

Return Value

Route

Route match(array|string $methods, string $uri, array|string|callable $action = null)

Register a new route with the given verbs.

Parameters

array|string $methods
string $uri
array|string|callable $action

Return Value

Route

void resources(array $resources, array $options = [])

Register an array of resource controllers.

Parameters

array $resources
array $options

Return Value

void

PendingResourceRegistration resource(string $name, string $controller, array $options = [])

Route a resource to a controller.

Parameters

string $name
string $controller
array $options

Return Value

PendingResourceRegistration

void apiResources(array $resources, array $options = [])

Register an array of API resource controllers.

Parameters

array $resources
array $options

Return Value

void

PendingResourceRegistration apiResource(string $name, string $controller, array $options = [])

Route an API resource to a controller.

Parameters

string $name
string $controller
array $options

Return Value

PendingResourceRegistration

void singletons(array $singletons, array $options = [])

Register an array of singleton resource controllers.

Parameters

array $singletons
array $options

Return Value

void

PendingSingletonResourceRegistration singleton(string $name, string $controller, array $options = [])

Route a singleton resource to a controller.

Parameters

string $name
string $controller
array $options

Return Value

PendingSingletonResourceRegistration

void apiSingletons(array $singletons, array $options = [])

Register an array of API singleton resource controllers.

Parameters

array $singletons
array $options

Return Value

void

PendingSingletonResourceRegistration apiSingleton(string $name, string $controller, array $options = [])

Route an API singleton resource to a controller.

Parameters

string $name
string $controller
array $options

Return Value

PendingSingletonResourceRegistration

void group(array $attributes, Closure|string $routes)

Create a route group with shared attributes.

Parameters

array $attributes
Closure|string $routes

Return Value

void

protected void updateGroupStack(array $attributes)

Update the group stack with the given attributes.

Parameters

array $attributes

Return Value

void

array mergeWithLastGroup(array $new, bool $prependExistingPrefix = true)

Merge the given array with the last group stack.

Parameters

array $new
bool $prependExistingPrefix

Return Value

array

protected void loadRoutes(Closure|string $routes)

Load the provided routes.

Parameters

Closure|string $routes

Return Value

void

string getLastGroupPrefix()

Get the prefix from the last group on the stack.

Return Value

string

Route addRoute(array|string $methods, string $uri, array|string|callable|null $action)

Add a route to the underlying route collection.

Parameters

array|string $methods
string $uri
array|string|callable|null $action

Return Value

Route

protected Route createRoute(array|string $methods, string $uri, mixed $action)

Create a new route instance.

Parameters

array|string $methods
string $uri
mixed $action

Return Value

Route

protected bool actionReferencesController(mixed $action)

Determine if the action is routing to a controller.

Parameters

mixed $action

Return Value

bool

protected array convertToControllerAction(array|string $action)

Add a controller based route action to the action array.

Parameters

array|string $action

Return Value

array

protected string prependGroupNamespace(string $class)

Prepend the last group namespace onto the use clause.

Parameters

string $class

Return Value

string

protected string prependGroupController(string $class)

Prepend the last group controller onto the use clause.

Parameters

string $class

Return Value

string

Route newRoute(array|string $methods, string $uri, mixed $action)

Create a new Route object.

Parameters

array|string $methods
string $uri
mixed $action

Return Value

Route

protected string prefix(string $uri)

Prefix the given URI with the last prefix.

Parameters

string $uri

Return Value

string

protected Route addWhereClausesToRoute(Route $route)

Add the necessary where clauses to the route based on its initial registration.

Parameters

Route $route

Return Value

Route

protected void mergeGroupAttributesIntoRoute(Route $route)

Merge the group stack with the controller action.

Parameters

Route $route

Return Value

void

Response respondWithRoute(string $name)

Return the response returned by the given route.

Parameters

string $name

Return Value

Response

Response dispatch(Request $request)

Dispatch the request to the application.

Parameters

Request $request

Return Value

Response

Response dispatchToRoute(Request $request)

Dispatch the request to a route and return the response.

Parameters

Request $request

Return Value

Response

protected Route findRoute(Request $request)

Find the route matching a given request.

Parameters

Request $request

Return Value

Route

protected Response runRoute(Request $request, Route $route)

Return the response for the given route.

Parameters

Request $request
Route $route

Return Value

Response

protected mixed runRouteWithinStack(Route $route, Request $request)

Run the given route within a Stack "onion" instance.

Parameters

Route $route
Request $request

Return Value

mixed

array gatherRouteMiddleware(Route $route)

Gather the middleware for the given route with resolved class names.

Parameters

Route $route

Return Value

array

array resolveMiddleware(array $middleware, array $excluded = [])

Resolve a flat array of middleware classes from the provided array.

Parameters

array $middleware
array $excluded

Return Value

array

protected array sortMiddleware(Collection $middlewares)

Sort the given middleware by priority.

Parameters

Collection $middlewares

Return Value

array

Response prepareResponse(Request $request, mixed $response)

Create a response instance from the given value.

Parameters

Request $request
mixed $response

Return Value

Response

static Response toResponse(Request $request, mixed $response)

Static version of prepareResponse.

Parameters

Request $request
mixed $response

Return Value

Response

Route substituteBindings(Route $route)

Substitute the route bindings onto the route.

Parameters

Route $route

Return Value

Route

Exceptions

Model>
BackedEnumCaseNotFoundException

void substituteImplicitBindings(Route $route)

Substitute the implicit route bindings for the given route.

Parameters

Route $route

Return Value

void

Exceptions

Model>
BackedEnumCaseNotFoundException

$this substituteImplicitBindingsUsing(callable $callback)

Register a callback to to run after implicit bindings are substituted.

Parameters

callable $callback

Return Value

$this

protected mixed performBinding(string $key, string $value, Route $route)

Call the binding callback for the given key.

Parameters

string $key
string $value
Route $route

Return Value

mixed

Exceptions

Model>

void matched(string|callable $callback)

Register a route matched event listener.

Parameters

string|callable $callback

Return Value

void

array getMiddleware()

Get all of the defined middleware short-hand names.

Return Value

array

$this aliasMiddleware(string $name, string $class)

Register a short-hand name for a middleware.

Parameters

string $name
string $class

Return Value

$this

bool hasMiddlewareGroup(string $name)

Check if a middlewareGroup with the given name exists.

Parameters

string $name

Return Value

bool

array getMiddlewareGroups()

Get all of the defined middleware groups.

Return Value

array

$this middlewareGroup(string $name, array $middleware)

Register a group of middleware.

Parameters

string $name
array $middleware

Return Value

$this

$this prependMiddlewareToGroup(string $group, string $middleware)

Add a middleware to the beginning of a middleware group.

If the middleware is already in the group, it will not be added again.

Parameters

string $group
string $middleware

Return Value

$this

$this pushMiddlewareToGroup(string $group, string $middleware)

Add a middleware to the end of a middleware group.

If the middleware is already in the group, it will not be added again.

Parameters

string $group
string $middleware

Return Value

$this

$this removeMiddlewareFromGroup(string $group, string $middleware)

Remove the given middleware from the specified group.

Parameters

string $group
string $middleware

Return Value

$this

$this flushMiddlewareGroups()

Flush the router's middleware groups.

Return Value

$this

void bind(string $key, string|callable $binder)

Add a new route parameter binder.

Parameters

string $key
string|callable $binder

Return Value

void

void model(string $key, string $class, Closure|null $callback = null)

Register a model binder for a wildcard.

Parameters

string $key
string $class
Closure|null $callback

Return Value

void

Closure getBindingCallback(string $key)

Get the binding callback for a given binding.

Parameters

string $key

Return Value

Closure

array getPatterns()

Get the global "where" patterns.

Return Value

array

void pattern(string $key, string $pattern)

Set a global where pattern on all routes.

Parameters

string $key
string $pattern

Return Value

void

void patterns(array $patterns)

Set a group of global where patterns on all routes.

Parameters

array $patterns

Return Value

void

bool hasGroupStack()

Determine if the router currently has a group stack.

Return Value

bool

array getGroupStack()

Get the current group stack for the router.

Return Value

array

mixed input(string $key, string|null $default = null)

Get a route parameter for the current route.

Parameters

string $key
string|null $default

Return Value

mixed

Request getCurrentRequest()

Get the request currently being dispatched.

Return Value

Request

Route|null getCurrentRoute()

Get the currently dispatched route instance.

Return Value

Route|null

Route|null current()

Get the currently dispatched route instance.

Return Value

Route|null

bool has(string|array $name)

Check if a route with the given name exists.

Parameters

string|array $name

Return Value

bool

string|null currentRouteName()

Get the current route name.

Return Value

string|null

bool is(mixed ...$patterns)

Alias for the "currentRouteNamed" method.

Parameters

mixed ...$patterns

Return Value

bool

bool currentRouteNamed(mixed ...$patterns)

Determine if the current route matches a pattern.

Parameters

mixed ...$patterns

Return Value

bool

string|null currentRouteAction()

Get the current route action.

Return Value

string|null

bool uses(array ...$patterns)

Alias for the "currentRouteUses" method.

Parameters

array ...$patterns

Return Value

bool

bool currentRouteUses(string $action)

Determine if the current route action matches a given action.

Parameters

string $action

Return Value

bool

void singularResourceParameters(bool $singular = true)

Set the unmapped global resource parameters to singular.

Parameters

bool $singular

Return Value

void

void resourceParameters(array $parameters = [])

Set the global resource parameter mapping.

Parameters

array $parameters

Return Value

void

array|null resourceVerbs(array $verbs = [])

Get or set the verbs used in the resource URIs.

Parameters

array $verbs

Return Value

array|null

RouteCollectionInterface getRoutes()

Get the underlying route collection.

void setRoutes(RouteCollection $routes)

Set the route collection instance.

Parameters

RouteCollection $routes

Return Value

void

void setCompiledRoutes(array $routes)

Set the compiled route collection instance.

Parameters

array $routes

Return Value

void

static array uniqueMiddleware(array $middleware)

Remove any duplicate middleware from the given array.

Parameters

array $middleware

Return Value

array

$this setContainer(Container $container)

Set the container instance used by the router.

Parameters

Container $container

Return Value

$this