Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 2.08 KB

File metadata and controls

46 lines (36 loc) · 2.08 KB

Middleware

Middleware are just methods that run before your code runs, be it a particular route or your whole application. Unlike many other frameworks and systems, Leaf gives you the opportunity to set global middleware that run before any and every route.

Before Route Middlewares

Leaf's Core router supports Before Route Middlewares, which are executed before the route handling is processed.

Like route handling functions, you hook a handling function to a combination of one or more HTTP request methods and a specific route pattern.

$leaf->before('GET|POST', '/admin/.*', function() {
    if (!isset($_SESSION['user'])) {
        header('location: /auth/login');
        exit();
    }
});

Unlike route handling functions, more than one before route middleware is executed when more than one route match is found.

Before Router Middlewares

Before route middlewares are route specific. Using a general route pattern (viz. all URLs), they can become Before Router Middlewares (in other projects sometimes referred to as before app middlewares) which are always executed, no matter what the requested URL is.

$leaf->before('GET', '/.*', function() {
    // ... this will always be executed
});

After Router Middleware / Run Callback

Run one (1) middleware function, name the After Router Middleware (in other projects sometimes referred to as after app middlewares) after the routing was processed. Just pass it along the $leaf->run() function. The run callback is route independent.

$leaf->run(function() {  });

Note: If the route handling function has exit()ed the run callback won't be run.



Request Response Session Environment Using a database


Built with ❤ by Mychi Darko