Router
extends Routable
in package
Routers are tools that allow your application to listen on alternative urls and attach controllers to different URLs than they would normally do. Please note that enabling a route to a certain controller does not disable it's canonical URL.
Tags
Table of Contents
- $children : Collection<string|int, Router>
- These routers inherit from this. Whenever this router is tasked with handling a request that it cannot satisfy itself, the router will delegate this request to it's children.
- $middleware : Collection<string|int, MiddlewareInterface>
- The middleware this router applies to it's routes and children. Middleware is applied once the Intent object is created and being returned.
- $prefix : string
- This is the url prefix the router is connected to. Every time the addRoute method is invoked, this router will prefix the route to scope it to the router.
- $routes : mixed
- __construct() : mixed
- addRoute() : Route
- This method adds a route to the current object. You can use this to customize a certain set of methods for the route to rewrite.
- delete() : Route
- This method adds a route that will only rewrite the request if it's method is DELETE. Otherwise the request will be ignored.
- findByName() : Route|null
- Finds a route by name, this searches recursively, so you don't need to.
- get() : Route
- This method adds a route that will only rewrite the request if it's method is GET. Otherwise the request will be ignored.
- getPrefix() : mixed
- getRoutes() : mixed
- middleware() : Router
- options() : Route
- Adds a route to reply to OPTIONS requests, these are most commonly used by user-agents when they attempt to perform pre-flight CORS tests.
- post() : Route
- This method adds a route that will only rewrite the request if it's method is POST. Otherwise the request will be ignored.
- put() : Route
- This method adds a route that will only rewrite the request if it's method is PUT. Otherwise the request will be ignored.
- request() : Route
- This method adds a route for any request that is sent either via GET or POST this are the most standard and common behaviors and the ones recommended over PUT or DELETE due to it's behavior.
- rewrite() : RouterResult
- This rewrites a request into a Path (or in given cases, a Response). This allows Spitfire to use the data from the Router to accordingly find a controller to handle the request being thrown at it.
- scope() : Router
Properties
$children
These routers inherit from this. Whenever this router is tasked with handling a request that it cannot satisfy itself, the router will delegate this request to it's children.
private
Collection<string|int, Router>
$children
This behavior implies that routes defined in the parent take precedence over the routes defined by it's children.
Also note: whenever you call the scope() method, the router generates a NEW Router for your scope. This means that you can have routers that manage routes within the same scope but have different middleware.
$middleware
The middleware this router applies to it's routes and children. Middleware is applied once the Intent object is created and being returned.
private
Collection<string|int, MiddlewareInterface>
$middleware
This means that more specific middleware is applied first when handling a request, and later when handling a response.
$prefix
This is the url prefix the router is connected to. Every time the addRoute method is invoked, this router will prefix the route to scope it to the router.
private
string
$prefix
Please note that forcing a router to accept a route that is not inside it's scope is likely to cause undefined behavior. Mostly because the router will reject every request that doesn't match it's namespace, but may generate urls that are outside it's scope.
$routes
private
mixed
$routes
Methods
__construct()
public
__construct(mixed $prefix) : mixed
Parameters
- $prefix : mixed
Return values
mixed —addRoute()
This method adds a route to the current object. You can use this to customize a certain set of methods for the route to rewrite.
public
addRoute(string $pattern, Closure|array<string|int, string> $target[, int $method = 0x3 ][, int $protocol = 0x3 ]) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
- $method : int = 0x3
-
The current method, default: Route::METHOD_GET | Route::METHOD_POST
- $protocol : int = 0x3
-
Whether the request s sent HTTP or HTTPS
Tags
Return values
Route —The route that is generated by this
delete()
This method adds a route that will only rewrite the request if it's method is DELETE. Otherwise the request will be ignored.
public
delete(string $pattern, Closure|array<string|int, string> $target) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
findByName()
Finds a route by name, this searches recursively, so you don't need to.
public
findByName(string $name) : Route|null
Since this method searches for the route, it may become taxing to your application if you have a ton of routes.
Parameters
- $name : string
Return values
Route|null —get()
This method adds a route that will only rewrite the request if it's method is GET. Otherwise the request will be ignored.
public
get(string $pattern, Closure|array<string|int, string> $target) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
getPrefix()
public
getPrefix() : mixed
Return values
mixed —getRoutes()
public
getRoutes() : mixed
Return values
mixed —middleware()
public
middleware(MiddlewareInterface $middleware) : Router
Parameters
- $middleware : MiddlewareInterface
Return values
Router —options()
Adds a route to reply to OPTIONS requests, these are most commonly used by user-agents when they attempt to perform pre-flight CORS tests.
public
options(string $pattern, Closure|array<string|int, string> $target) : Route
Options requests should therefore not be treated the same way as GET, POST or HEAD requests, since they do not assume that the user agent is enforcing CORS policies when sending them.
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
post()
This method adds a route that will only rewrite the request if it's method is POST. Otherwise the request will be ignored.
public
post(string $pattern, Closure|array<string|int, string> $target) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
put()
This method adds a route that will only rewrite the request if it's method is PUT. Otherwise the request will be ignored.
public
put(string $pattern, Closure|array<string|int, string> $target) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
request()
This method adds a route for any request that is sent either via GET or POST this are the most standard and common behaviors and the ones recommended over PUT or DELETE due to it's behavior.
public
request(string $pattern, Closure|array<string|int, string> $target) : Route
Parameters
- $pattern : string
-
A route valid pattern @link http://www.spitfirephp.com/wiki/index.php/Router/Patterns
- $target : Closure|array<string|int, string>
-
Where the content will be redirected to @link http://www.spitfirephp.com/wiki/index.php/Router/Target
Return values
Route —The route that is generated by this
rewrite()
This rewrites a request into a Path (or in given cases, a Response). This allows Spitfire to use the data from the Router to accordingly find a controller to handle the request being thrown at it.
public
rewrite(string $url, string $method, string $protocol) : RouterResult
Please note that Spitfire is 'lazy' about it's routes. Once it found a valid one that can be used to respond to the request it will stop looking for another possible rewrite.
Parameters
- $url : string
- $method : string
- $protocol : string
Tags
Return values
RouterResult —scope()
public
scope(string $scope[, Closure $do = null ]) : Router
Parameters
- $scope : string
- $do : Closure = null