Leaf PHP

App extends Router
in package

Leaf PHP Framework -------- The easiest way to build simple but powerful apps and APIs quickly.

Tags
author

Michael Darko mickdd22@gmail.com

copyright

2019-2025 Michael Darko

link
https://leafphp.dev
license

MIT

Table of Contents

Properties

$appRoutes  : mixed
Sorted list of routes and their handlers
$downHandler  : mixed
Callable to be invoked if app is down
$errorHandler  : Run
Callable to be invoked on application error
$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

__call()  : mixed
__construct()  : mixed
Constructor
__get()  : mixed
__isset()  : mixed
__set()  : mixed
__unset()  : mixed
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.
attach()  : mixed
Run code that can change the behaviour of Leaf *Usually used by library creators*
attachView()  : mixed
Attach a view engine to Leaf
config()  : mixed
Configure Leaf Settings
cors()  : mixed
Evade CORS errors
csrf()  : mixed
Add CSRF protection to your app
delete()  : mixed
Add a route with DELETE method
environment()  : mixed
Run mode-specific code. Unlike script, this runs immediately.
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
halt()  : mixed
Halt
handleUrl()  : mixed
Force call the Leaf URL handler
head()  : mixed
Add a route with HEAD method
headers()  : Headers
Get the Request Headers
hook()  : mixed
Add a router hook
inertia()  : mixed
Add a route that renders an inertia view
logger()  : Log|null|void
Get application log
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
register()  : mixed
This method adds a method to the global leaf instance Register a method and use it globally on the Leaf Object
registerMiddleware()  : mixed
Register a middleware in your Leaf application by name
request()  : Request
Get the Request object
resource()  : mixed
Create a resource route for using controllers.
response()  : Response
Get the Response object
root()  : string
Get the absolute path to this Leaf application's root directory
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
script()  : mixed
Create mode-specific code
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.
setErrorHandler()  : mixed
Set a custom error screen.
setNamespace()  : mixed
Set a global namespace for your handlers
use()  : mixed
Add middleware
view()  : mixed
Add a route that renders a view
vite()  : mixed
Tune vite to work without Leaf MVC
ws()  : mixed
Create a route handled by websocket (requires Eien module)
loadConfig()  : mixed
mapHandler()  : array<string|int, mixed>
Map handler and options
setupErrorHandler()  : mixed
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
setupDefaultContainer()  : 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

$errorHandler

Callable to be invoked on application error

protected Run $errorHandler

$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

__call()

public __call(mixed $method, mixed $args) : mixed
Parameters
$method : mixed
$args : mixed

__construct()

Constructor

public __construct([array<string|int, mixed> $userSettings = [] ]) : mixed
Parameters
$userSettings : array<string|int, mixed> = []

Associative array of application settings

__get()

public __get(mixed $name) : mixed
Parameters
$name : mixed

__isset()

public __isset(mixed $name) : mixed
Parameters
$name : mixed

__set()

public __set(mixed $name, mixed $value) : mixed
Parameters
$name : mixed
$value : mixed

__unset()

public __unset(mixed $name) : mixed
Parameters
$name : mixed

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

attach()

Run code that can change the behaviour of Leaf *Usually used by library creators*

public attach(callable $code) : mixed
Parameters
$code : callable

attachView()

Attach a view engine to Leaf

public attachView(mixed $view[, mixed $name = null ]) : mixed
Parameters
$view : mixed

The view engine to attach

$name : mixed = null

config()

Configure Leaf Settings

public config(string|array<string|int, mixed> $name[, mixed $value = null ]) : mixed

This method defines application settings and acts as a setter and a getter.

If only one argument is specified and that argument is a string, the value of the setting identified by the first argument will be returned, or NULL if that setting does not exist.

If only one argument is specified and that argument is an associative array, the array will be merged into the existing application settings.

If two arguments are provided, the first argument is the name of the setting to be created or updated, and the second argument is the setting value.

Parameters
$name : string|array<string|int, mixed>

If a string, the name of the setting to set or retrieve. Else an associated array of setting names and values

$value : mixed = null

If name is a string, the value of the setting identified by $name

Return values
mixed

The value of a setting if only one argument is a string

cors()

Evade CORS errors

public cors([mixed $options = [] ]) : mixed
Parameters
$options : mixed = []

Config for cors

csrf()

Add CSRF protection to your app

public csrf([array<string|int, mixed> $options = [] ]) : mixed
Parameters
$options : array<string|int, mixed> = []

Config for csrf

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

environment()

Run mode-specific code. Unlike script, this runs immediately.

public static environment(string $mode, callable $callback) : mixed
Parameters
$mode : string

The mode to run code in

$callback : callable

The code to run in selected mode.

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
string

getCurrentUri()

Define the current relative URI.

public static getCurrentUri() : string
Return values
string

getNamespace()

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

halt()

Halt

public static halt(int $status[, string $message = '' ]) : mixed

Stop the application and immediately send the response with a specific status and body to the HTTP client. This may send any type of response: info, success, redirect, client error, or server error.

Parameters
$status : int

The HTTP response status

$message : string = ''

The HTTP response body

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

logger()

Get application log

public logger() : Log|null|void
Return values
Log|null|void

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

register()

This method adds a method to the global leaf instance Register a method and use it globally on the Leaf Object

public register(mixed $name, mixed $value) : mixed
Parameters
$name : mixed
$value : mixed

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

root()

Get the absolute path to this Leaf application's root directory

public root() : string

This method returns the absolute path to the Leaf application's directory. If the Leaf application is installed in a public-accessible sub-directory, the sub-directory path will be included. This method will always return an absolute path WITH a trailing slash.

Return values
string

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
string

routes()

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
Tags
inheritdoc

script()

Create mode-specific code

public static script(string $mode, callable $callback) : mixed
Parameters
$mode : string

The mode to run code in

$callback : callable

The code to run in selected mode.

set404()

Set the 404 handling function.

public static set404([object|callable $handler = null ]) : mixed
Parameters
$handler : object|callable = null

The function to be executed

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

setErrorHandler()

Set a custom error screen.

public setErrorHandler(mixed $handler) : mixed
Parameters
$handler : mixed

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

vite()

Tune vite to work without Leaf MVC

public vite([mixed $options = ['assets' => '', 'build' => '', 'hotFile' => 'hot'] ]) : mixed
Parameters
$options : mixed = ['assets' => '', 'build' => '', 'hotFile' => 'hot']

ws()

Create a route handled by websocket (requires Eien module)

public ws(string $name, callable $callback) : mixed
Parameters
$name : string

The url of the route

$callback : callable

The callback function

Tags
uses
package

Eien module

see
https://leafphp.dev/modules/eien/

loadConfig()

protected loadConfig([array<string|int, mixed> $userSettings = [] ]) : mixed
Parameters
$userSettings : array<string|int, mixed> = []

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>

setupErrorHandler()

protected setupErrorHandler() : 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 = []

setupDefaultContainer()

private setupDefaultContainer() : mixed

        
On this page

Search results