FastSitePHP\Route

When [$app->get()], [$app->post()] and other methods are called a new Route Object is created.

Source Code

GitHub

Exemple de Code

Use Route Filters

// Routes can have custom filter functions assigned to them to run specific
// code if a route is matched, perform validation, or another task required
// by your site. Filter functions only run if the route is matched to the
// requested URL.

// Define some callback/closure functions
$text_response = function() use ($app) {
    $app->header('Content-Type', 'text/plain');
};
$is_authenticated = function() {
    // Check User Permissions ...
    return true;
};

// When routes are created [get(), route(), post(), etc], the created route
// is returned so you can call [filter()] after defining the route.
// This page will be returned as Plain Text page because the filter function
// sets the Response Header and returns no value.
$app->get('/text-page', function($name) {
    return 'Hello';
})->filter($text_response);

// A route can have multiple filters and for clarity you may want to put
// filter functions on separate lines. This page will only be called if
// [$is_authenticated] returns [true] and it will also be a text response.
$app->get('/secure-text-page', function($name) {
    return 'Hello ' . $name;
})
->filter($is_authenticated)
->filter($text_response);

// The [filter()] function also accepts a string representing
// a class and method in the format of 'Class.method'.
$app->get('/phpinfo', function($name) {
    phpinfo();
})
->filter('Env.isLocalhost');

// When using string filters you can specify a root namespace
// for the classes using the App property [middleware_root].
$app->middleware_root = 'App\Middleware';

Properties

Name Data Type Default Description
pattern string null URL Pattern to match for the route to get called
controller string
\Closure
null Controller Closure function or string that refers to a class or class and method.
method string
null
null Request Method to match ['GET', 'POST', etc]
filter_callbacks array [] Array of filter functions for the route

Methods

filter($callback)

Add a filter function to the route. If the route is matched then all filter functions for it are called. If one or more of the filter functions returns [false] then the route is skipped. Filter functions are not required to return anything. If a filter function returns a Response Object then it will be sent to the client and the controller for the route will not be called.

Returns: $this