Overview
Understanding Routing
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.
In order to use Leaf's router in your project, you have to imoprt it
Using a different router
Leaf uses a router component which is based as Leaf's core component....however, this router is seperated from the rest of Leaf itself, so, you can tie in any routing library of your choice.
1. Install itCreating Routes
You can define application routes using proxy methods on the Leaf\Core\Leaf instance. Leaf supports a whole bunch of HTTP methods, see all supported methods.GET requests
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
POST requests
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
Using Post Params
View Request for more info on handling paramsPUT requests
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
DELETE requests
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
OPTIONS requests
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
PATCH requests
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
ALL requests
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
Route "Hooking"
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
Running your routes
After setting all the routes, you'll need to dispatch the routes. This is achieved through Leaf's run() method.
Next Steps
RequestResponse
Named Parameters
Handling 404