Router

class Router implements HttpKernelInterface, RouteFiltererInterface (View source)

Properties

protected Dispatcher $events The event dispatcher instance.
protected Container $container The IoC container instance.
protected RouteCollection $routes The route collection instance.
protected Route $current The currently dispatched route instance.
protected Request $currentRequest The request currently being dispatched.
protected ControllerDispatcher $controllerDispatcher The controller dispatcher instance.
protected ControllerInspector $inspector The controller inspector instance.
protected bool $filtering Indicates if the router is running filters.
protected array $patternFilters The registered pattern based filters.
protected array $regexFilters The registered regular expression based filters.
protected array $binders The registered route value binders.
protected array $patterns The globally available parameter patterns.
protected array $groupStack The route group attribute stack.
static array $verbs All of the verbs supported by the router.
protected array $resourceDefaults The default actions for a resourceful controller.

Methods

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

Create a new Router instance.

Route get(string $uri, Closure|array|string $action)

Register a new GET route with the router.

Route post(string $uri, Closure|array|string $action)

Register a new POST route with the router.

Route put(string $uri, Closure|array|string $action)

Register a new PUT route with the router.

Route patch(string $uri, Closure|array|string $action)

Register a new PATCH route with the router.

Route delete(string $uri, Closure|array|string $action)

Register a new DELETE route with the router.

Route options(string $uri, Closure|array|string $action)

Register a new OPTIONS route with the router.

Route any(string $uri, Closure|array|string $action)

Register a new route responding to all verbs.

Route match(array|string $methods, string $uri, Closure|array|string $action)

Register a new route with the given verbs.

void controllers(array $controllers)

Register an array of controllers with wildcard routing.

void controller(string $uri, string $controller, array $names = array())

Route a controller to a URI with wildcard routing.

void registerInspected(array $route, string $controller, string $method, array $names)

Register an inspected controller route.

void addFallthroughRoute(string $controller, string $uri)

Add a fallthrough route for a controller.

void resource(string $name, string $controller, array $options = array())

Route a resource to a controller.

void prefixedResource(string $name, string $controller, array $options)

Build a set of prefixed resource routes.

array getResourcePrefix(string $name)

Extract the resource and prefix from a resource name.

array getResourceMethods(array $defaults, array $options)

Get the applicable resource methods.

string getResourceUri(string $resource)

Get the base resource URI for a given resource.

string getNestedResourceUri(array $segments)

Get the URI for a nested resource segment array.

array getResourceAction(string $resource, string $controller, string $method, array $options)

Get the action array for a resource route.

string getResourceName(string $resource, string $method, array $options)

Get the name for a given resource.

string getGroupResourceName(string $prefix, string $resource, string $method)

Get the resource name for a grouped resource.

string getResourceWildcard(string $value)

Format a resource wildcard for usage.

Route addResourceIndex(string $name, string $base, string $controller, array $options)

Add the index method for a resourceful route.

Route addResourceCreate(string $name, string $base, string $controller, array $options)

Add the create method for a resourceful route.

Route addResourceStore(string $name, string $base, string $controller, array $options)

Add the store method for a resourceful route.

Route addResourceShow(string $name, string $base, string $controller, array $options)

Add the show method for a resourceful route.

Route addResourceEdit(string $name, string $base, string $controller, array $options)

Add the edit method for a resourceful route.

void addResourceUpdate(string $name, string $base, string $controller, array $options)

Add the update method for a resourceful route.

Route addPutResourceUpdate(string $name, string $base, string $controller, array $options)

Add the update method for a resourceful route.

void addPatchResourceUpdate(string $name, string $base, string $controller)

Add the update method for a resourceful route.

Route addResourceDestroy(string $name, string $base, string $controller, array $options)

Add the destroy method for a resourceful route.

void group(array $attributes, Closure $callback)

Create a route group with shared attributes.

void updateGroupStack(array $attributes)

Update the group stack with the given attributes.

array mergeWithLastGroup(array $new)

Merge the given array with the last group stack.

static array mergeGroup(array $new, array $old)

Merge the given group attributes.

static string formatUsesPrefix(array $new, array $old)

Format the uses prefix for the new group attributes.

static string formatGroupPrefix(array $new, array $old)

Format the prefix for the new group attributes.

string getLastGroupPrefix()

Get the prefix from the last group on the stack.

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

Add a route to the underlying route collection.

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

Create a new route instance.

Route 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.

Route addWhereClausesToRoute(Route $route)

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

void mergeController(Route $route)

Merge the group stack with the controller action.

bool routingToController(array $action)

Determine if the action is routing to a controller.

array getControllerAction(array|string $action)

Add a controller based route action to the action array.

Closure getClassClosure(string $controller)

Get the Closure for a controller based action.

string prependGroupUses(string $uses)

Prepend the last group uses onto the use clause.

Response dispatch(Request $request)

Dispatch the request to the application.

mixed dispatchToRoute(Request $request)

Dispatch the request to a route and return the response.

Route findRoute(Request $request)

Find the route matching a given request.

Route substituteBindings(Route $route)

Substitute the route bindings onto the route.

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.

void before(string|callable $callback)

Register a new "before" filter with the router.

void after(string|callable $callback)

Register a new "after" filter with the router.

void addGlobalFilter(string $filter, string|callable $callback)

Register a new global filter with the router.

void filter(string $name, mixed $callback)

Register a new filter with the router.

mixed parseFilter(callable|string $callback)

Parse the registered filter.

void when(string $pattern, string $name, array|null $methods = null)

Register a pattern-based filter with the router.

void whenRegex(string $pattern, string $name, array|null $methods = null)

Register a regular expression based filter with the router.

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

Register a model binder for a wildcard.

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

Add a new route parameter binder.

Closure createClassBinding(string $binding)

Create a class based binding using the IoC container.

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

mixed callFilter(string $filter, Request $request, Response $response = null)

Call the given filter with the request and response.

mixed callRouteBefore(Route $route, Request $request)

Call the given route's before filters.

mixed|null callPatternFilters(Route $route, Request $request)

Call the pattern based filters for the request.

array findPatternFilters(Request $request)

Find the patterned filters matching a request.

array patternsByMethod(string $method, array $filters)

Filter pattern filters that don't apply to the request verb.

bool filterSupportsMethod(array $filter, array $method)

Determine if the given pattern filters applies to a given method.

mixed callAttachedBefores(Route $route, Request $request)

Call the given route's before (non-pattern) filters.

mixed callRouteAfter(Route $route, Request $request, Response $response)

Call the given route's before filters.

mixed callRouteFilter(string $filter, array $parameters, Route $route, Request $request, Response|null $response = null)

Call the given route filter.

array cleanFilterParameters(array $parameters)

Clean the parameters being passed to a filter callback.

Response prepareResponse(Request $request, mixed $response)

Create a response instance from the given value.

void withoutFilters(callable $callback)

Run a callback with filters disable on the router.

void enableFilters()

Enable route filtering on the router.

void disableFilters()

Disable route filtering on the router.

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

Get a route parameter for the current route.

Route getCurrentRoute()

Get the currently dispatched route instance.

Route current()

Get the currently dispatched route instance.

bool has(string $name)

Check if a route with the given name exists.

string|null currentRouteName()

Get the current route name.

bool is()

Alias for the "currentRouteNamed" method.

bool currentRouteNamed(string $name)

Determine if the current route matches a given name.

string|null currentRouteAction()

Get the current route action.

bool uses()

Alias for the "currentRouteUses" method.

bool currentRouteUses(string $action)

Determine if the current route action matches a given action.

Request getCurrentRequest()

Get the request currently being dispatched.

RouteCollection getRoutes()

Get the underlying route collection.

ControllerDispatcher getControllerDispatcher()

Get the controller dispatcher instance.

void setControllerDispatcher(ControllerDispatcher $dispatcher)

Set the controller dispatcher instance.

ControllerInspector getInspector()

Get a controller inspector instance.

array getPatterns()

Get the global "where" patterns.

Response handle(Request $request, int $type = HttpKernelInterface::MASTER_REQUEST, bool $catch = true)

Get the response for a given request.

Details

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

Create a new Router instance.

Parameters

Dispatcher $events
Container $container

Return Value

void

Route get(string $uri, Closure|array|string $action)

Register a new GET route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route post(string $uri, Closure|array|string $action)

Register a new POST route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route put(string $uri, Closure|array|string $action)

Register a new PUT route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route patch(string $uri, Closure|array|string $action)

Register a new PATCH route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route delete(string $uri, Closure|array|string $action)

Register a new DELETE route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route options(string $uri, Closure|array|string $action)

Register a new OPTIONS route with the router.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route any(string $uri, Closure|array|string $action)

Register a new route responding to all verbs.

Parameters

string $uri
Closure|array|string $action

Return Value

Route

Route match(array|string $methods, string $uri, Closure|array|string $action)

Register a new route with the given verbs.

Parameters

array|string $methods
string $uri
Closure|array|string $action

Return Value

Route

void controllers(array $controllers)

Register an array of controllers with wildcard routing.

Parameters

array $controllers

Return Value

void

void controller(string $uri, string $controller, array $names = array())

Route a controller to a URI with wildcard routing.

Parameters

string $uri
string $controller
array $names

Return Value

void

protected void registerInspected(array $route, string $controller, string $method, array $names)

Register an inspected controller route.

Parameters

array $route
string $controller
string $method
array $names

Return Value

void

protected void addFallthroughRoute(string $controller, string $uri)

Add a fallthrough route for a controller.

Parameters

string $controller
string $uri

Return Value

void

void resource(string $name, string $controller, array $options = array())

Route a resource to a controller.

Parameters

string $name
string $controller
array $options

Return Value

void

protected void prefixedResource(string $name, string $controller, array $options)

Build a set of prefixed resource routes.

Parameters

string $name
string $controller
array $options

Return Value

void

protected array getResourcePrefix(string $name)

Extract the resource and prefix from a resource name.

Parameters

string $name

Return Value

array

protected array getResourceMethods(array $defaults, array $options)

Get the applicable resource methods.

Parameters

array $defaults
array $options

Return Value

array

string getResourceUri(string $resource)

Get the base resource URI for a given resource.

Parameters

string $resource

Return Value

string

protected string getNestedResourceUri(array $segments)

Get the URI for a nested resource segment array.

Parameters

array $segments

Return Value

string

protected array getResourceAction(string $resource, string $controller, string $method, array $options)

Get the action array for a resource route.

Parameters

string $resource
string $controller
string $method
array $options

Return Value

array

protected string getResourceName(string $resource, string $method, array $options)

Get the name for a given resource.

Parameters

string $resource
string $method
array $options

Return Value

string

protected string getGroupResourceName(string $prefix, string $resource, string $method)

Get the resource name for a grouped resource.

Parameters

string $prefix
string $resource
string $method

Return Value

string

string getResourceWildcard(string $value)

Format a resource wildcard for usage.

Parameters

string $value

Return Value

string

protected Route addResourceIndex(string $name, string $base, string $controller, array $options)

Add the index method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected Route addResourceCreate(string $name, string $base, string $controller, array $options)

Add the create method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected Route addResourceStore(string $name, string $base, string $controller, array $options)

Add the store method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected Route addResourceShow(string $name, string $base, string $controller, array $options)

Add the show method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected Route addResourceEdit(string $name, string $base, string $controller, array $options)

Add the edit method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected void addResourceUpdate(string $name, string $base, string $controller, array $options)

Add the update method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

void

protected Route addPutResourceUpdate(string $name, string $base, string $controller, array $options)

Add the update method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

protected void addPatchResourceUpdate(string $name, string $base, string $controller)

Add the update method for a resourceful route.

Parameters

string $name
string $base
string $controller

Return Value

void

protected Route addResourceDestroy(string $name, string $base, string $controller, array $options)

Add the destroy method for a resourceful route.

Parameters

string $name
string $base
string $controller
array $options

Return Value

Route

void group(array $attributes, Closure $callback)

Create a route group with shared attributes.

Parameters

array $attributes
Closure $callback

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)

Merge the given array with the last group stack.

Parameters

array $new

Return Value

array

static array mergeGroup(array $new, array $old)

Merge the given group attributes.

Parameters

array $new
array $old

Return Value

array

static protected string formatUsesPrefix(array $new, array $old)

Format the uses prefix for the new group attributes.

Parameters

array $new
array $old

Return Value

string

static protected string formatGroupPrefix(array $new, array $old)

Format the prefix for the new group attributes.

Parameters

array $new
array $old

Return Value

string

protected string getLastGroupPrefix()

Get the prefix from the last group on the stack.

Return Value

string

protected Route addRoute(array|string $methods, string $uri, Closure|array|string $action)

Add a route to the underlying route collection.

Parameters

array|string $methods
string $uri
Closure|array|string $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 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 mergeController(Route $route)

Merge the group stack with the controller action.

Parameters

Route $route

Return Value

void

protected bool routingToController(array $action)

Determine if the action is routing to a controller.

Parameters

array $action

Return Value

bool

protected array getControllerAction(array|string $action)

Add a controller based route action to the action array.

Parameters

array|string $action

Return Value

array

protected Closure getClassClosure(string $controller)

Get the Closure for a controller based action.

Parameters

string $controller

Return Value

Closure

protected string prependGroupUses(string $uses)

Prepend the last group uses onto the use clause.

Parameters

string $uses

Return Value

string

Response dispatch(Request $request)

Dispatch the request to the application.

Parameters

Request $request

Return Value

Response

mixed dispatchToRoute(Request $request)

Dispatch the request to a route and return the response.

Parameters

Request $request

Return Value

mixed

protected Route findRoute(Request $request)

Find the route matching a given request.

Parameters

Request $request

Return Value

Route

protected Route substituteBindings(Route $route)

Substitute the route bindings onto the route.

Parameters

Route $route

Return Value

Route

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

void matched(string|callable $callback)

Register a route matched event listener.

Parameters

string|callable $callback

Return Value

void

void before(string|callable $callback)

Register a new "before" filter with the router.

Parameters

string|callable $callback

Return Value

void

void after(string|callable $callback)

Register a new "after" filter with the router.

Parameters

string|callable $callback

Return Value

void

protected void addGlobalFilter(string $filter, string|callable $callback)

Register a new global filter with the router.

Parameters

string $filter
string|callable $callback

Return Value

void

void filter(string $name, mixed $callback)

Register a new filter with the router.

Parameters

string $name
mixed $callback

Return Value

void

protected mixed parseFilter(callable|string $callback)

Parse the registered filter.

Parameters

callable|string $callback

Return Value

mixed

void when(string $pattern, string $name, array|null $methods = null)

Register a pattern-based filter with the router.

Parameters

string $pattern
string $name
array|null $methods

Return Value

void

void whenRegex(string $pattern, string $name, array|null $methods = null)

Register a regular expression based filter with the router.

Parameters

string $pattern
string $name
array|null $methods

Return Value

void

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

Register a model binder for a wildcard.

Parameters

string $key
string $class
Closure $callback

Return Value

void

Exceptions

NotFoundHttpException

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

Add a new route parameter binder.

Parameters

string $key
string|callable $binder

Return Value

void

Closure createClassBinding(string $binding)

Create a class based binding using the IoC container.

Parameters

string $binding

Return Value

Closure

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

protected mixed callFilter(string $filter, Request $request, Response $response = null)

Call the given filter with the request and response.

Parameters

string $filter
Request $request
Response $response

Return Value

mixed

mixed callRouteBefore(Route $route, Request $request)

Call the given route's before filters.

Parameters

Route $route
Request $request

Return Value

mixed

protected mixed|null callPatternFilters(Route $route, Request $request)

Call the pattern based filters for the request.

Parameters

Route $route
Request $request

Return Value

mixed|null

array findPatternFilters(Request $request)

Find the patterned filters matching a request.

Parameters

Request $request

Return Value

array

protected array patternsByMethod(string $method, array $filters)

Filter pattern filters that don't apply to the request verb.

Parameters

string $method
array $filters

Return Value

array

protected bool filterSupportsMethod(array $filter, array $method)

Determine if the given pattern filters applies to a given method.

Parameters

array $filter
array $method

Return Value

bool

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

Call the given route's before (non-pattern) filters.

Parameters

Route $route
Request $request

Return Value

mixed

mixed callRouteAfter(Route $route, Request $request, Response $response)

Call the given route's before filters.

Parameters

Route $route
Request $request
Response $response

Return Value

mixed

mixed callRouteFilter(string $filter, array $parameters, Route $route, Request $request, Response|null $response = null)

Call the given route filter.

Parameters

string $filter
array $parameters
Route $route
Request $request
Response|null $response

Return Value

mixed

protected array cleanFilterParameters(array $parameters)

Clean the parameters being passed to a filter callback.

Parameters

array $parameters

Return Value

array

protected Response prepareResponse(Request $request, mixed $response)

Create a response instance from the given value.

Parameters

Request $request
mixed $response

Return Value

Response

void withoutFilters(callable $callback)

Run a callback with filters disable on the router.

Parameters

callable $callback

Return Value

void

void enableFilters()

Enable route filtering on the router.

Return Value

void

void disableFilters()

Disable route filtering on the router.

Return Value

void

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

Get a route parameter for the current route.

Parameters

string $key
string $default

Return Value

mixed

Route getCurrentRoute()

Get the currently dispatched route instance.

Return Value

Route

Route current()

Get the currently dispatched route instance.

Return Value

Route

bool has(string $name)

Check if a route with the given name exists.

Parameters

string $name

Return Value

bool

string|null currentRouteName()

Get the current route name.

Return Value

string|null

bool is()

Alias for the "currentRouteNamed" method.

Return Value

bool

bool currentRouteNamed(string $name)

Determine if the current route matches a given name.

Parameters

string $name

Return Value

bool

string|null currentRouteAction()

Get the current route action.

Return Value

string|null

bool uses()

Alias for the "currentRouteUses" method.

Return Value

bool

bool currentRouteUses(string $action)

Determine if the current route action matches a given action.

Parameters

string $action

Return Value

bool

Request getCurrentRequest()

Get the request currently being dispatched.

Return Value

Request

RouteCollection getRoutes()

Get the underlying route collection.

Return Value

RouteCollection

ControllerDispatcher getControllerDispatcher()

Get the controller dispatcher instance.

Return Value

ControllerDispatcher

void setControllerDispatcher(ControllerDispatcher $dispatcher)

Set the controller dispatcher instance.

Parameters

ControllerDispatcher $dispatcher

Return Value

void

ControllerInspector getInspector()

Get a controller inspector instance.

Return Value

ControllerInspector

array getPatterns()

Get the global "where" patterns.

Return Value

array

Response handle(Request $request, int $type = HttpKernelInterface::MASTER_REQUEST, bool $catch = true)

Get the response for a given request.

Parameters

Request $request
int $type
bool $catch

Return Value

Response

© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/4.2/Illuminate/Routing/Router.html