Router
in package
Leaf Router --------------- Super simple and powerful routing with Leaf
Tags
Table of Contents
Properties
- $appRoutes : mixed
- Sorted list of routes and their handlers
- $downHandler : mixed
- Callable to be invoked if app is down
- $groupRoute : mixed
- Current group base path
- $hooks : mixed
- 'Middleware' to run at specific times
- $middleware : mixed
- All middleware that should be run
- $namedMiddleware : mixed
- Named middleware
- $namedRoutes : mixed
- All named routes
- $namespace : mixed
- Default controller namespace
- $notFoundHandler : mixed
- Callable to be invoked if no matching routes are found
- $routeGroupMiddleware : mixed
- Route based middleware
- $routes : mixed
- All added routes and their handlers
- $serverBasePath : mixed
- The Server Base Path for Router Execution
Methods
- all() : mixed
- Add a route with all available HTTP methods
- apiResource() : mixed
- Create a resource route for using controllers without the create and edit actions.
- delete() : mixed
- Add a route with DELETE method
- findRoute() : array<string|int, mixed>
- Find the current route
- get() : mixed
- Add a route with GET method
- getBasePath() : string
- Return server base Path, and define it if isn't defined.
- getCurrentUri() : string
- Define the current relative URI.
- getNamespace() : string
- Get the global handler namespace.
- getRoute() : array<string|int, mixed>
- Get route info of the current route
- group() : mixed
- Alias for mount
- handleUrl() : mixed
- Force call the Leaf URL handler
- head() : mixed
- Add a route with HEAD method
- hook() : mixed
- Add a router hook
- inertia() : mixed
- Add a route that renders an inertia view
- match() : mixed
- Store a route and it's handler
- mount() : mixed
- Mounts a collection of callbacks onto a base route.
- options() : mixed
- Add a route with OPTIONS method
- patch() : mixed
- Add a route with PATCH method
- post() : mixed
- Add a route with POST method
- push() : mixed
- Redirect to another route
- put() : mixed
- Add a route with PUT method
- redirect() : mixed
- Add a route that sends an HTTP redirect
- registerMiddleware() : mixed
- Register a middleware in your Leaf application by name
- resource() : mixed
- Create a resource route for using controllers.
- route() : string
- Get route url by defined route name
- routes() : array<string|int, mixed>
- Get all routes registered in your leaf app
- run() : mixed
- Dispatch your application routes
- set404() : mixed
- Set the 404 handling function.
- setBasePath() : mixed
- Explicilty sets the server base path. To be used when your entry script path differs from your entry URLs.
- setDown() : mixed
- Set a custom maintenance mode callback.
- setNamespace() : mixed
- Set a global namespace for your handlers
- use() : mixed
- Add middleware
- view() : mixed
- Add a route that renders a view
- mapHandler() : array<string|int, mixed>
- Map handler and options
- callHook() : mixed
- Call a router hook
- handle() : int
- Handle a set of routes: if a match is found, execute the relating handling function.
- invoke() : mixed
Properties
$appRoutes
Sorted list of routes and their handlers
protected
static mixed
$appRoutes
= []
$downHandler
Callable to be invoked if app is down
protected
static mixed
$downHandler
$groupRoute
Current group base path
protected
static mixed
$groupRoute
= ''
$hooks
'Middleware' to run at specific times
protected
static mixed
$hooks
= ['router.before' => false, 'router.before.route' => false, 'router.after.route' => false, 'router.after' => false]
$middleware
All middleware that should be run
protected
static mixed
$middleware
= []
$namedMiddleware
Named middleware
protected
static mixed
$namedMiddleware
= []
$namedRoutes
All named routes
protected
static mixed
$namedRoutes
= []
$namespace
Default controller namespace
protected
static mixed
$namespace
= ''
$notFoundHandler
Callable to be invoked if no matching routes are found
protected
static mixed
$notFoundHandler
$routeGroupMiddleware
Route based middleware
protected
static mixed
$routeGroupMiddleware
= []
$routes
All added routes and their handlers
protected
static mixed
$routes
= []
$serverBasePath
The Server Base Path for Router Execution
protected
static mixed
$serverBasePath
= ''
Methods
all()
Add a route with all available HTTP methods
public
static all(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
apiResource()
Create a resource route for using controllers without the create and edit actions.
public
static apiResource(string $pattern, array<string|int, mixed>|string $controller) : mixed
This creates a routes that implement CRUD functionality in a controller
/posts
creates:
/posts
- GET | HEAD - Controller@index/posts
- POST - Controller@store/posts/{id}
- GET | HEAD - Controller@show/posts/{id}/edit
- POST | PUT | PATCH - Controller@update/posts/{id}/delete
- POST | DELETE - Controller@destroy
Parameters
- $pattern : string
-
The base route to use eg: /post
- $controller : array<string|int, mixed>|string
-
to handle route eg: PostController
delete()
Add a route with DELETE method
public
static delete(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
findRoute()
Find the current route
public
static findRoute([array<string|int, mixed>|null $routes = null ][, string|null $uri = null ][, mixed $returnFirst = true ]) : array<string|int, mixed>
Parameters
- $routes : array<string|int, mixed>|null = null
- $uri : string|null = null
- $returnFirst : mixed = true
Return values
array<string|int, mixed>get()
Add a route with GET method
public
static get(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
getBasePath()
Return server base Path, and define it if isn't defined.
public
static getBasePath() : string
Return values
stringgetCurrentUri()
Define the current relative URI.
public
static getCurrentUri() : string
Return values
stringgetNamespace()
Get the global handler namespace.
public
static getNamespace() : string
Return values
string —The given namespace if exists
getRoute()
Get route info of the current route
public
static getRoute() : array<string|int, mixed>
Return values
array<string|int, mixed> —The route info array
group()
Alias for mount
public
static group(string $path, callable|array<string|int, mixed> $handler) : mixed
Parameters
- $path : string
-
The route sub pattern/path to mount the callbacks on
- $handler : callable|array<string|int, mixed>
-
The callback method
handleUrl()
Force call the Leaf URL handler
public
static handleUrl(string $method, string $url) : mixed
Parameters
- $method : string
-
The method to call
- $url : string
-
The uri to force
head()
Add a route with HEAD method
public
static head(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
hook()
Add a router hook
public
static hook(string $name, callable|null $handler) : mixed
Available hooks
- router.before
- router.before.route
- router.before.dispatch
- router.after.dispatch
- router.after.route
- router.after
Parameters
- $name : string
-
The hook to set
- $handler : callable|null
-
The hook handler
inertia()
Add a route that renders an inertia view
public
static inertia(string $pattern, string $view[, array<string|int, mixed> $data = [] ]) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $view : string
-
The view to render
- $data : array<string|int, mixed> = []
-
The data to pass to the view
match()
Store a route and it's handler
public
static match(string $allowedMethods, string $pattern, string|array<string|int, mixed>|callable $handler) : mixed
Parameters
- $allowedMethods : string
- $pattern : string
-
The route pattern/path to match
- $handler : string|array<string|int, mixed>|callable
-
The handler for route when matched
mount()
Mounts a collection of callbacks onto a base route.
public
static mount(string $path, callable|array<string|int, mixed> $handler) : mixed
Parameters
- $path : string
-
The route sub pattern/path to mount the callbacks on
- $handler : callable|array<string|int, mixed>
-
The callback method
options()
Add a route with OPTIONS method
public
static options(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
patch()
Add a route with PATCH method
public
static patch(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
post()
Add a route with POST method
public
static post(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
push()
Redirect to another route
public
static push(string|array<string|int, mixed> $route[, array<string|int, mixed>|null $data = null ]) : mixed
Use Leaf\Http\Response::redirect
instead
Parameters
- $route : string|array<string|int, mixed>
-
The route to redirect to
- $data : array<string|int, mixed>|null = null
-
Data to pass to the next route
put()
Add a route with PUT method
public
static put(string $pattern, mixed $handler) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $handler : mixed
redirect()
Add a route that sends an HTTP redirect
public
static redirect(string $from, string $to[, int $status = 302 ]) : mixed
Parameters
- $from : string
-
The url to redirect from
- $to : string
-
The url to redirect to
- $status : int = 302
-
The http status code for redirect
registerMiddleware()
Register a middleware in your Leaf application by name
public
registerMiddleware(string $name, callable $middleware) : mixed
Parameters
- $name : string
-
The name of the middleware
- $middleware : callable
-
The middleware to register
resource()
Create a resource route for using controllers.
public
static resource(string $pattern, array<string|int, mixed>|string $controller) : mixed
This creates a routes that implement CRUD functionality in a controller
/posts
creates:
/posts
- GET | HEAD - Controller@index/posts
- POST - Controller@store/posts/{id}
- GET | HEAD - Controller@show/posts/create
- GET | HEAD - Controller@create/posts/{id}/edit
- GET | HEAD - Controller@edit/posts/{id}/edit
- POST | PUT | PATCH - Controller@update/posts/{id}/delete
- POST | DELETE - Controller@destroy
Parameters
- $pattern : string
-
The base route to use eg: /post
- $controller : array<string|int, mixed>|string
-
to handle route eg: PostController
route()
Get route url by defined route name
public
static route(string $routeName[, array<string|int, mixed>|string|null $params = null ]) : string
Parameters
- $routeName : string
- $params : array<string|int, mixed>|string|null = null
Return values
stringroutes()
Get all routes registered in your leaf app
public
static routes() : array<string|int, mixed>
Return values
array<string|int, mixed>run()
Dispatch your application routes
public
static run([callable|null $callback = null ]) : mixed
Parameters
- $callback : callable|null = null
set404()
Set the 404 handling function.
public
static set404([object|callable $handler = null ]) : mixed
Parameters
- $handler : object|callable = null
-
The function to be executed
setBasePath()
Explicilty sets the server base path. To be used when your entry script path differs from your entry URLs.
public
static setBasePath(mixed $serverBasePath) : mixed
Parameters
- $serverBasePath : mixed
Tags
setDown()
Set a custom maintenance mode callback.
public
static setDown([callable|null $handler = null ]) : mixed
Parameters
- $handler : callable|null = null
-
The function to be executed
setNamespace()
Set a global namespace for your handlers
public
static setNamespace(string $namespace) : mixed
Parameters
- $namespace : string
-
The global namespace to set
use()
Add middleware
public
static use(callable|string $middleware) : mixed
This method prepends new middleware to the application middleware stack. The argument must be an instance that subclasses Leaf_Middleware.
Parameters
- $middleware : callable|string
-
The middleware to set
view()
Add a route that renders a view
public
static view(string $pattern, string $view[, array<string|int, mixed> $data = [] ]) : mixed
Parameters
- $pattern : string
-
The route pattern/path to match
- $view : string
-
The view to render
- $data : array<string|int, mixed> = []
-
The data to pass to the view
mapHandler()
Map handler and options
protected
static mapHandler(mixed $handler[, mixed $options = ['name' => null, 'middleware' => null, 'namespace' => null] ]) : array<string|int, mixed>
Parameters
- $handler : mixed
- $options : mixed = ['name' => null, 'middleware' => null, 'namespace' => null]
Return values
array<string|int, mixed>callHook()
Call a router hook
private
static callHook(string $name) : mixed
Parameters
- $name : string
-
The hook to call
handle()
Handle a set of routes: if a match is found, execute the relating handling function.
private
static handle([array<string|int, mixed> $routes = null ][, bool $quitAfterRun = false ][, string|null $uri = null ]) : int
Parameters
- $routes : array<string|int, mixed> = null
-
Collection of route patterns and their handling functions
- $quitAfterRun : bool = false
-
Does the handle function need to quit after one route was matched?
- $uri : string|null = null
-
The URI to call (automatically set if nothing is passed).
Return values
int —The number of routes handled
invoke()
private
static invoke(mixed $handler[, mixed $params = [] ]) : mixed
Parameters
- $handler : mixed
- $params : mixed = []