As explained before, Leaf uses a single root file, to which all the server requests are redirected. Leaf then takes these requests and matches them to rules you have defined. The results are then displayed to the user. It's actually a very simple concept.
The router module is tied directly to Leaf Core, so once you initialise leeaf, you can use routing
$leaf = new Leaf\App();
Although Leaf provides you with a default router, you are free to import and use any router you want.
- Install whatever you want
composer require imaginary/router
- Import and use it in your project
$leaf = new Leaf\App();
// initialise imaginary router
$imr = new Imaginary\Router();
// you can still use leaf modules
$imr->get("/", function() use($leaf) {
$leaf->response->respond(["title" => "hello"]);
});
Back to Leaf's router, You can define application routes using proxy methods on the Leaf\App instance. Leaf supports different types of requests, let's look at them.
You can add a route that handles only GET HTTP requests with the Leaf router's get() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->get('/home', function() {
// your code
});
You can add a route that handles only POST HTTP requests with the Leaf router's post() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->post('/users/add', function() use($request) {
$user = $request->get('user');
// create a new user
});
Using Post Params View Request for more info on handling params
You can add a route that handles only PUT HTTP requests with the Leaf router’s put() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->put('/book/edit/{id}', function($id) {
// your code
});
You can add a route that handles only DELETE HTTP requests with the Leaf router's delete() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->delete('/quotes/{id}', function($id) {
// delete quote
});
You can add a route that handles only OPTIONS HTTP requests with the Leaf router's options() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->options('/quotes/{id}', function($id) {
// return headers
});
You can add a route that handles only PATCH HTTP requests with the Leaf router's patch() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->patch('/post/{id}', function($id) {
// your code
});
You can add a route that handles all HTTP requests with the Leaf router's all() method. It accepts two arguments:
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->all('/post/{id}', function($id) {
// your code
});
You can add a route that handles a couple of HTTP methods with the Leaf router's match() method. It accepts three arguments:
- The HTTP method(s) seperated by |
- The route pattern (with optional named placeholders or PCRE based patterns)
- The route callback
$leaf->match('GET|POST', '/people', function() {
// your code
});
After setting all the routes, you'll need to dispatch the routes. This is achieved through Leaf's run() method.
$leaf->run();
Leaf's core router has specially prepared for 404 errors, and is bent on giving users full control over displaying this error
For this reason, we've prepared the set404() method. In version 2, you can just call set404 without passing in any function, this will set the 404 handler to the default Leaf 404 page. You can change this at any time by passing in your custom page
// will use default Leaf 404 page(new in v2.0)
$leaf->set404();
// custom 404 page
$leaf->set404(function() use($leaf) {
$leaf->response->renderPage("./pages/404.html");
});
Request Response Session Environment Using a database
Built with ❤ by Mychi Darko