Skip to content

Routing

Leaf MVC, Leaf API and Skeleton all rely on Leaf's powerful routing engine. The only difference is that routing the MVC way makes use of controllers instead of callable functions. Everything else works the same way. This document will cover the basics of Leaf MVC routing.

Leaf Routing

If you're not familiar with Leaf's routing, you should read the Leaf Routing document first.

The Routes Folder

Leaf MVC, Leaf API and Skeleton all have a routes folder in which all your application's routes are defined. The routes folder is located in the app directory in Leaf MVC and Leaf API but is located in the root directory in Skeleton. The routes folder contains a single file called index.php which is where all your application's routes are defined. This file is automatically loaded by Leaf when your application starts.

Linking Controllers

Controllers provide a way to organize your application's logic. They are a great way to keep your routes file clean and easy to read. Controllers are just classes that implement the App\Controllers\Controller protocol. You can find more information about controllers in the Controllers document. This document will focus on how to link controllers to routes.

Leaf provides a simple interface for interacting with controllers and their methods from inside your routes. The idea is to run a method inside your controller whenever a route is matched. This is done by telling Leaf which controller to use and which method to run. Leaf will then create an instance of the controller and run the method.

Example

Let's say you have a controller called App\Controllers\HomeController and you want to run the index method inside it whenever the / route is matched. You can do this by passing a string containing your controller name and method to your route. The string should be in the format controllerName@methodName.

$app->get('/', '\App\Controllers\HomeController@index');
app()->get('/', '\App\Controllers\HomeController@index');

Controller Namespaces

In case you're using an auto loader or using leaf in another framework and you have your controllers in another directory, you can do sommething like this

app()->get('/(\d+)', '\App\Controllers\User@showProfile');
$app->get('/(\d+)', '\App\Controllers\User@showProfile');

But this gets tedious if you have a lot of routes. So Leaf allows you to set a "general" namespace, you can set the default namespace to use on your router instance via setNamespace()

app()->setNamespace('\App\Controllers');

app()->get('/users/(\d+)', 'User@showProfile');
app()->get('/cars/(\d+)', 'Car@showProfile');
$app->setNamespace('\App\Controllers');

$app->get('/users/(\d+)', 'User@showProfile');
$app->get('/cars/(\d+)', 'Car@showProfile');

This has already been setup for you in the root routes file of your MVC application.

Next Steps

Follow along with the next steps to learn more about Leaf MVC.

Routing has loaded