Skip to content

Routing

iamdual edited this page Sep 13, 2024 · 26 revisions

In the routing system, we specify the request path, method and the middleware we want from app/Config/routes.php file.

An example route config:

return static function (RouteCollector $routing) {
    $routing->route('/', [Main::class, 'index']);

    $routing->group('/book', function () use ($routing) {
        $routing->get('/', [BookMain::class, 'index']);
        $routing->get('/([0-9]+)', [Details::class, 'index']);
        $routing->post('/([0-9]+)', [Actions::class, 'add']);
        $routing->delete('/([0-9]+)', [Actions::class, 'delete'], middleware: [Authenticate::class]);
    });

    $routing->group('/user', function () use ($routing) {
        $routing->get('/([0-9]+)', [Profile::class, 'details']);
        $routing->group('/message', function () use ($routing) {
            $routing->post('/([0-9]+)', [Message::class, 'send']);
        });
    }, middleware: [Authenticate::class]);
};

Based on the above example, if we were to visualize the routes, it would look like this:

Path Handler Method Middleware(s)
/ Main->index() Any
/book BookMain->index() GET
/book/2 Details->index() GET
/book/2 Actions->add() POST
/book/2 Actions->delete() DELETE Authenticate
/user/2 Profile->details() GET Authenticate
/user/message/2 Message->send() POST Authenticate

Clone this wiki locally