Skip to content

Routing in Leaf MVC

Routing is at the heart of every web application, mapping URLs to functionality. Leaf’s powerful router keeps this process simple and intuitive, helping you define routes with minimal effort—like a smart traffic controller seamlessly directing requests.

Route partials

In Leaf MVC, all routes are defined in partials within the app/routes directory. Partials are simple PHP files prefixed with _, like _auth.php or _api.php. There’s no special syntax—just a clean, structured way to keep your code organized as your app or API scales.

To add a new route, simply place it in the relevant partial or create a new one if it doesn’t fit into an existing group. This keeps your routing intuitive and easy to manage.

Leaf MVC is just like Leaf is as unopinionated as it gets, so if you are anti-partials, you can define all your routes in the `app/routes/index.php` file.

Breaking down routes

Every 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 (a controller method or a function)
php
app()->get('/home', 'HomeController@index');

This is the general syntax for defining a route in Leaf. The first argument is the route pattern, and the second argument is the route handler. Since we are using MVC, the route handler for all your routes will be controller methods, but Leaf always allows you to quickly define a route with a closure.

php
app()->get('/home', function() {
    // Your code here
});

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
php
app()->post('/users/add', 'UsersController@store');

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
php
app()->put('/book/edit/{id}', 'BooksController@update');

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
php
app()->delete('/quotes/{id}', 'QuotesController@destroy');

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
php
app()->patch('/quotes/{id}', 'QuotesController@update');

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
php
app()->match('GET|POST', '/users', 'UsersController@index');

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
php
app()->view('/home', 'home');

Named routes

In larger applications, managing routes efficiently is key. Leaf lets you name routes, so you can reference them by name instead of hardcoding URLs, making updates easier. You can also define options like middleware using an array as the second argument when setting up a route, keeping your code flexible and maintainable.

php
app()->get('/home', ['name' => 'home', 'HomeController@index']);

You can then redirect to this route using the route name by passing an array with the route name to the redirect() method.

php
response()->redirect(['home']);

If you want to get details about a route using its name, you can use the route() method.

php
$route = app()->route($routeName);

This will return an array containing the following information:

  • pattern: The route pattern
  • path: The route path
  • name: The route name
  • method: The route method
  • handler: 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.

HomeController.php
php
...

public function index() {
    $route = app()->getRoute();
    return response()->json($route);
}

This method returns an array containing the following information:

  • pattern: The route pattern
  • path: The route path
  • name: The route name
  • method: The route method
  • handler: The route handler
  • params: Dynamic route parameters

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.

php
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.

php
response()->redirect(['home']);

Updating Error Screens

If you need to set up custom error responses, you can do so in the app/routes/index.php file. We have loaded examples for 404 and 500 error pages. You can customize these to your liking. For example, we can set up JSON responses for these errors:

php
/*
|--------------------------------------------------------------------------
| Set up 404 handler
|--------------------------------------------------------------------------
|
| Leaf provides a default 404 page, but you can also create your
| own 404 handler by calling app()->set404(). Whatever function
| you set will be called when a 404 error is encountered
|
*/
app()->set404(function () {
  response()->json('Resource not found', 404, true);
});

/*
|--------------------------------------------------------------------------
| Set up 500 handler
|--------------------------------------------------------------------------
|
| Leaf provides a default 500 page, but you can create your own error
| 500 handler by calling the setErrorHandler() method. The function
| you set will be called when a 500 error is encountered
|
*/
app()->setErrorHandler(function () {
  response()->json('An error occurred, our team has been notified', 500, true);
});

Now that you understand how to define routes in Leaf, you can learn more about Leaf's routing system, including how to use middleware, dynamic routing, or explore other topics in the documentation.

  • Middleware

    Add middleware to your routes to perform tasks before or after a request is handled.

  • Dynamic routing

    Learn how to process incoming requests, handle form submissions, and more.

  • Using Controllers

    Learn how to handle incoming requests and responses using controllers.

  • Frontend

    Learn about SSR, SPA, and how to use Leaf with your favorite frontend framework.

Released under the MIT License.