Routing
Routing is the foundation of every web application. It's the process of defining the URL structure of your application and how it responds to requests. Leaf comes with a powerful router that simplifies the way you define routes in your application. You can take routing as one fancy traffic officer that directs traffic to the right place.
Create a route
Using Leaf MVC?
We've crafted a specialized guide for routing in Leaf MVC. While it's similar to the basic routing in Leaf, it's more detailed and tailored for Leaf MVC.
Start buildingEvery route has a URL (the web address the user visits) and an HTTP method (like GET, POST, etc.), which tells the server what action to take. For example, if you create a route for a GET request to /home
, the user can access that page by visiting http://example.com/home
. This way, different URLs and methods control how users interact with your app.
So to define a route, you need to specify the URL and the HTTP method. Leaf router allows you to do this using get()
, post()
, put()
, patch()
, delete()
, ... methods. Let's take a look at them.
Create a GET route
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
- The route handler
app()->get('/home', function () {
// your code
});
Create a POST route
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
- The route handler
app()->post('/users/add', function () {
$user = request()->get('user');
// create a new user
});
Create a PUT route
The put()
method allows you to add a route that handles only PUT
HTTP requests. It accepts two arguments:
- The route pattern
- The route handler
app()->put('/book/edit/{id}', function ($id) {
// your code
});
Create a DELETE route
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
- The route handler
app()->delete('/quotes/{id}', function ($id) {
// delete quote
});
Create a PATCH route
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
- The route handler
app()->patch('/quotes/{id}', function ($id) {
// update quote
});
Create a multiple method route
There are some cases where you want a route to handle multiple HTTP methods. You can do this using the match()
method. This method accepts three arguments:
- A list of HTTP methods separated by | (pipe)
- The route pattern
- The route handler
app()->match('GET|POST', '/users', function () {
// your code
});
Create a view route
If your route needs to return a template without any logic, you can use the view()
method. This method accepts two arguments:
- The route pattern
- The view file to render
app()->view('/home', 'home');
The view()
method will look for the view file using whatever view engine you have set up in your app. For instance, if you have blade setup, it will look for a file called home.blade.php
. The template location also depends on your view engine setup.
Running your routes
After defining all the routes you application needs, you need to start the router to listen for incoming requests. You can do this by calling the run()
method.
app()->run();
Named routes
In big applications, you might have to reference a route over and over again. When you change the route URL, you'll have to change it everywhere you referenced it. To avoid this, you can name your routes and reference them by their name. This will save you a lot of time and prevent errors.
Leaf router allows you name routes by using route params. They allow you add extra options to your routes like a route name, middleware, etc. You can set route options by passing an array with configuration options as the second argument to the whatever route you are working on.
app()->get('/home', ['name' => 'home', function () {
// your code
}]);
You can then redirect to this route using the route name by passing an array with the route name to the redirect()
method.
response()->redirect(['home']);
If you want to get details about a route using its name, you can use the route()
method.
$route = app()->route($routeName);
This will return an array containing the following information:
pattern
: The route patternpath
: The route pathname
: The route namemethod
: The route methodhandler
: The route handler- Any other route options
Getting the current route
There are times when you need to get the current route which the user is visiting from inside your route handler. You can do this by calling the getRoute()
method on the router instance.
app()->get('/home', ['name' => 'home', function () {
$route = app()->getRoute();
echo $route['name'];
}]);
This method returns an array containing the following information:
pattern
: The route patternpath
: The route pathname
: The route namemethod
: The route methodhandler
: The route handlerparams
: Dynamic route parameters
Navigating to another route
There are times when you need to redirect users to another route. For example, after a user logs in, you might want to redirect them to their dashboard. You can do this by calling the redirect()
method on the response instance.
response()->redirect('/login');
If your route has a name, you can navigate to it by passing the route name in an array to the redirect()
method.
response()->redirect(['home']);