Provides features for building routes inside scopes.
Gives an easy to use way to build routes and append them into a route collection.
string '[0-9]+'
Regular expression for auto increment IDs
string '[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}' Regular expression for UUIDs
Cake\Routing\RouteCollectionThe route collection routes should be added to.
array<string>The extensions that should be set into the routes connected.
stringName prefix for connected routes.
arrayThe scope parameters if there are any.
stringThe path prefix scope that this collection uses.
array<string, array>Default HTTP request method => controller action map.
stringDefault route class to use if none is provided in connect() options.
array<string>The list of middleware that routes in this builder get added during construction.
Constructor
Create a route object, or return the provided object.
Helper to create routes that only respond to a single HTTP method.
Add additional extensions to what is already in current scope
Apply a middleware to the current route scope.
Connects a new Route.
Create a route that only responds to DELETE requests.
Connect the /{controller} and /{controller}/{action}/* fallback routes.
Create a route that only responds to GET requests.
Get the extensions in this route builder's scope.
Get the middleware that this builder will apply to routes.
Get default route class.
Create a route that only responds to HEAD requests.
Load routes from a plugin.
Apply a set of middleware to a group
Checks if there is already a route with a given name.
Get/set the name prefix for this scope.
Create a route that only responds to OPTIONS requests.
Get the parameter names/values for this scope.
Parse the defaults if they're a string
Create a route that only responds to PATCH requests.
Get the path this scope is for.
Add plugin routes.
Create a route that only responds to POST requests.
Add prefixed routes.
Create a route that only responds to PUT requests.
Connects a new redirection Route in the router.
Register a middleware with the RouteCollection.
Generate REST resource routes for the given controller(s).
Create a new routing scope.
Set the extensions in this route builder's scope.
Set default route class.
__construct(Cake\Routing\RouteCollection $collection, string $path, array $params = [], array<string, mixed> $options = [])
Constructor
routeClass - The default route class to use when adding routes.extensions - The extensions to connect when adding routes.namePrefix - The prefix to prepend to all route names.middleware - The names of the middleware routes should have applied.Cake\Routing\RouteCollection $collection The route collection to append routes into.
string $path The path prefix the scope is for.
array $params optional The scope's routing parameters.
array<string, mixed> $options optional Options list.
_makeRoute(Cake\Routing\Route\Route|string $route, array $defaults, array<string, mixed> $options): Cake\Routing\Route\Route
Create a route object, or return the provided object.
Cake\Routing\Route\Route|string $route The route template or route object.
array $defaults Default parameters.
array<string, mixed> $options Additional options parameters.
Cake\Routing\Route\RouteInvalidArgumentExceptionBadMethodCallException_methodRoute(string $method, string $template, array|string $target, string|null $name): Cake\Routing\Route\Route
Helper to create routes that only respond to a single HTTP method.
string $method The HTTP method name to match.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name The name of the route.
Cake\Routing\Route\RouteaddExtensions(array<string>|string $extensions): $this
Add additional extensions to what is already in current scope
array<string>|string $extensions One or more extensions to add
$thisapplyMiddleware(string ...$names): $this
Apply a middleware to the current route scope.
Requires middleware to be registered via registerMiddleware()
string ...$names The names of the middleware to apply to the current scope.
$thisRuntimeExceptionconnect(Cake\Routing\Route\Route|string $route, array|string $defaults = [], array<string, mixed> $options = []): Cake\Routing\Route\Route
Connects a new Route.
Routes are a way of connecting request URLs to objects in your application. At their core routes are a set or regular expressions that are used to match requests to destinations.
Examples:
$routes->connect('/{controller}/{action}/*'); The first parameter will be used as a controller name while the second is used as the action name. The '/*' syntax makes this route greedy in that it will match requests like /posts/index as well as requests like /posts/edit/1/foo/bar.
$routes->connect('/home-page', ['controller' => 'Pages', 'action' => 'display', 'home']); The above shows the use of route parameter defaults. And providing routing parameters for a static route.
$routes->connect(
'/{lang}/{controller}/{action}/{id}',
[],
['id' => '[0-9]+', 'lang' => '[a-z]{3}']
); Shows connecting a route with custom route parameters as well as providing patterns for those parameters. Patterns for routing parameters do not need capturing groups, as one will be added for each route params.
$options offers several 'special' keys that have special meaning in the $options array.
routeClass is used to extend and change how individual routes parse requests and handle reverse routing, via a custom routing class. Ex. 'routeClass' => 'SlugRoute'
pass is used to define which of the routed parameters should be shifted into the pass array. Adding a parameter to pass will remove it from the regular route array. Ex. 'pass' => ['slug'].persist is used to define which route parameters should be automatically included when generating new URLs. You can override persistent parameters by redefining them in a URL or remove them by setting the parameter to false. Ex. 'persist' => ['lang']
multibytePattern Set to true to enable multibyte pattern support in route parameter patterns._name is used to define a specific name for routes. This can be used to optimize reverse routing lookups. If undefined a name will be generated for each connected route._ext is an array of filename extensions that will be parsed out of the url if present. See {@link \Cake\Routing\RouteCollection::setExtensions()}._method Only match requests with specific HTTP verbs._host - Define the host name pattern if you want this route to only match specific host names. You can use .* and to create wildcard subdomains/hosts e.g. *.example.com matches all subdomains on example.com.Example of using the _method condition:
$routes->connect('/tasks', ['controller' => 'Tasks', 'action' => 'index', '_method' => 'GET']); The above route will only be matched for GET requests. POST requests will fail to match this route.
Cake\Routing\Route\Route|string $route A string describing the template of the route
array|string $defaults optional An array describing the default route parameters. These parameters will be used by default and can supply routing parameters that are not dynamic. See above.
array<string, mixed> $options optional An array matching the named elements in the route to regular expressions which that element should match. Also contains additional parameters such as which routed parameters should be shifted into the passed arguments, supplying patterns for routing parameters and supplying the name of a custom routing class.
Cake\Routing\Route\RouteInvalidArgumentExceptionBadMethodCallExceptiondelete(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to DELETE requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\Routefallbacks(string|null $routeClass = null): $this
Connect the /{controller} and /{controller}/{action}/* fallback routes.
This is a shortcut method for connecting fallback routes in a given scope.
string|null $routeClass optional the route class to use, uses the default routeClass if not specified
$thisget(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to GET requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\RoutegetExtensions(): array<string>
Get the extensions in this route builder's scope.
array<string>getMiddleware(): array
Get the middleware that this builder will apply to routes.
arraygetRouteClass(): string
Get default route class.
stringhead(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to HEAD requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\RouteloadPlugin(string $name): $this
Load routes from a plugin.
The routes file will have a local variable named $routes made available which contains the current RouteBuilder instance.
string $name The plugin name
$thisCake\Core\Exception\MissingPluginExceptionInvalidArgumentExceptionmiddlewareGroup(string $name, array<string> $middlewareNames): $this
Apply a set of middleware to a group
string $name Name of the middleware group
array<string> $middlewareNames Names of the middleware
$thisnameExists(string $name): bool
Checks if there is already a route with a given name.
string $name Name.
boolnamePrefix(string|null $value = null): string
Get/set the name prefix for this scope.
Modifying the name prefix will only change the prefix used for routes connected after the prefix is changed.
string|null $value optional Either the value to set or null.
stringoptions(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to OPTIONS requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\Routeparams(): array
Get the parameter names/values for this scope.
arrayparseDefaults(array|string $defaults): array
Parse the defaults if they're a string
array|string $defaults Defaults array from the connect() method.
arraypatch(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to PATCH requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\Routepath(): string
Get the path this scope is for.
stringplugin(string $name, callable|array $options = [], callable|null $callback = null): $this
Add plugin routes.
This method creates a new scoped route collection that includes relevant plugin information.
The plugin name will be inflected to the underscore version to create the routing path. If you want a custom path name, use the path option.
Routes connected in the scoped collection will have the correct path segment prepended, and have a matching plugin routing key set.
path The path prefix to use. Defaults to Inflector::dasherize($name)._namePrefix Set a prefix used for named routes. The prefix is prepended to the name of any route created in a scope callback.string $name The plugin name to build routes for
callable|array $options optional Either the options to use, or a callback to build routes.
callable|null $callback optional The callback to invoke that builds the plugin routes Only required when $options is defined.
$thispost(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to POST requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\Routeprefix(string $name, callable|array $params = [], callable|null $callback = null): $this
Add prefixed routes.
This method creates a scoped route collection that includes relevant prefix information.
The $name parameter is used to generate the routing parameter name. For example a path of admin would result in 'prefix' => 'admin' being applied to all connected routes.
You can re-open a prefix as many times as necessary, as well as nest prefixes. Nested prefixes will result in prefix values like admin/api which translates to the Controller\Admin\Api\ namespace.
If you need to have prefix with dots, eg: '/api/v1.0', use 'path' key for $params argument:
$route->prefix('Api', function($route) {
$route->prefix('V10', ['path' => '/v1.0'], function($route) {
// Translates to `Controller\Api\V10\` namespace
});
}); string $name The prefix name to use.
callable|array $params optional An array of routing defaults to add to each connected route. If you have no parameters, this argument can be a callable.
callable|null $callback optional The callback to invoke that builds the prefixed routes.
$thisInvalidArgumentExceptionput(string $template, array|string $target, string|null $name = null): Cake\Routing\Route\Route
Create a route that only responds to PUT requests.
string $template The URL template to use.
array|string $target An array describing the target route parameters. These parameters should indicate the plugin, prefix, controller, and action that this route points to.
string|null $name optional The name of the route.
Cake\Routing\Route\Routeredirect(string $route, array|string $url, array<string, mixed> $options = []): Cake\Routing\Route\RouteCake\Routing\Route\RedirectRoute
Connects a new redirection Route in the router.
Redirection routes are different from normal routes as they perform an actual header redirection if a match is found. The redirection can occur within your application or redirect to an outside location.
Examples:
$routes->redirect('/home/*', ['controller' => 'Posts', 'action' => 'view']); Redirects /home/* to /posts/view and passes the parameters to /posts/view. Using an array as the redirect destination allows you to use other routes to define where a URL string should be redirected to.
$routes->redirect('/posts/*', 'http://google.com', ['status' => 302]); Redirects /posts/* to http://google.com with a HTTP status of 302
status Sets the HTTP status (default 301)persist Passes the params to the redirected route, if it can. This is useful with greedy routes, routes that end in * are greedy. As you can remap URLs and not lose any passed args.string $route A string describing the template of the route
array|string $url A URL to redirect to. Can be a string or a Cake array-based URL
array<string, mixed> $options optional An array matching the named elements in the route to regular expressions which that element should match. Also contains additional parameters such as which routed parameters should be shifted into the passed arguments. As well as supplying patterns for routing parameters.
Cake\Routing\Route\RouteCake\Routing\Route\RedirectRouteregisterMiddleware(string $name, Psr\Http\Server\MiddlewareInterfaceClosure|string $middleware): $this
Register a middleware with the RouteCollection.
Once middleware has been registered, it can be applied to the current routing scope or any child scopes that share the same RouteCollection.
string $name The name of the middleware. Used when applying middleware to a scope.
Psr\Http\Server\MiddlewareInterfaceClosure|string $middleware The middleware to register.
$thisresources(string $name, callable|array $options = [], callable|null $callback = null): $this
Generate REST resource routes for the given controller(s).
A quick way to generate a default routes to a set of REST resources (controller(s)).
Connect resource routes for an app controller:
$routes->resources('Posts'); Connect resource routes for the Comments controller in the Comments plugin:
Router::plugin('Comments', function ($routes) {
$routes->resources('Comments');
}); Plugins will create lowercase dasherized resource routes. e.g /comments/comments
Connect resource routes for the Articles controller in the Admin prefix:
Router::prefix('Admin', function ($routes) {
$routes->resources('Articles');
}); Prefixes will create lowercase dasherized resource routes. e.g /admin/posts
You can create nested resources by passing a callback in:
$routes->resources('Articles', function ($routes) {
$routes->resources('Comments');
}); The above would generate both resource routes for /articles, and /articles/{article_id}/comments. You can use the map option to connect additional resource methods:
$routes->resources('Articles', [
'map' => ['deleteAll' => ['action' => 'deleteAll', 'method' => 'DELETE']]
]); In addition to the default routes, this would also connect a route for /articles/delete_all. By default, the path segment will match the key name. You can use the 'path' key inside the resource definition to customize the path name.
You can use the inflect option to change how path segments are generated:
$routes->resources('PaymentTypes', ['inflect' => 'underscore']); Will generate routes like /payment-types instead of /payment_types
/posts
string $name A controller name to connect resource routes for.
callable|array $options optional Options to use when generating REST routes, or a callback.
callable|null $callback optional An optional callback to be executed in a nested scope. Nested scopes inherit the existing path and 'id' parameter.
$thisscope(string $path, callable|array $params, callable|null $callback = null): $this
Create a new routing scope.
Scopes created with this method will inherit the properties of the scope they are added to. This means that both the current path and parameters will be appended to the supplied parameters.
_namePrefix Set a prefix used for named routes. The prefix is prepended to the name of any route created in a scope callback.string $path The path to create a scope for.
callable|array $params Either the parameters to add to routes, or a callback.
callable|null $callback optional The callback to invoke that builds the plugin routes. Only required when $params is defined.
$thisInvalidArgumentExceptionsetExtensions(array<string>|string $extensions): $this
Set the extensions in this route builder's scope.
Future routes connected in through this builder will have the connected extensions applied. However, setting extensions does not modify existing routes.
array<string>|string $extensions The extensions to set.
$thissetRouteClass(string $routeClass): $this
Set default route class.
string $routeClass Class name.
$thisThe route collection routes should be added to.
Cake\Routing\RouteCollectionThe extensions that should be set into the routes connected.
array<string>Name prefix for connected routes.
stringThe scope parameters if there are any.
arrayThe path prefix scope that this collection uses.
stringDefault HTTP request method => controller action map.
array<string, array>Default route class to use if none is provided in connect() options.
stringThe list of middleware that routes in this builder get added during construction.
array<string>
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Routing.RouteBuilder.html