"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Organizing Your Routes Modularly and Automatically in Lithe

Organizing Your Routes Modularly and Automatically in Lithe

Published on 2024-12-23
Browse:718

Organizando Suas Rotas de Forma Modular e Automática no Lithe

Organizing routes in an application can be a challenge, especially when the number of pages grows. With that in mind, in Lithe, you can organize your routes in a cleaner and more modular way using the set('routes', ...) method. With it, the route registration process becomes simpler, allowing you to focus on the logic of your application, while the system takes care of everything automatically.

How does it work?

When you use set('routes', ...) in Lithe, the system automatically finds and loads all PHP files within the routes folder (including subfolders). Each route file will be mapped based on its structure, creating routes with specific paths.

For example:

  • If you have a cart.php file, the route will be /cart.
  • If you have an admin/dashboard.php file, the route will be /admin/dashboard.

Caution When Using Subfolders

Within the route structure in Lithe, the index.php file is always interpreted as the main route of a folder. In other words, if you have an index.php file inside the routes folder, it will be mapped to the / route, which represents the application root.

However, if you use subfolders like panel/index.php, the system will not map to the /panel route, but to /panel/index. To ensure that the route maps correctly to /panel, simply name the file panel.php, like this:

  • index.php → maps to route /
  • panel.php → maps to the /panel route

This approach helps avoid route overload and makes the file structure much more intuitive.

Directory Structure

Here is an example of what the directory structure might look like:

/routes
    cart.php
    checkout.php
    /admin
        dashboard.php
        users.php

How to Define Routes

In each route file, you can use whatever code style you prefer, either with functional syntax or classical syntax.

Example of cart.php:

get('/', function ($req, $res) { 
  $res->send('Carrinho'); 
});

Example of admin/dashboard.php:

$router->get('/', function ($req, $res) { 
  $res->send('Painel Administrativo'); 
});

Application Configuration

To configure the path of your routes and enable automatic loading in Lithe, simply add the following line of code to your application:

$app->set('routes', __DIR__ . '/routes');  // Define o caminho e carrega rotas automaticamente

With this configuration, the system will automatically locate and load all defined routes, simplifying the process and ensuring a much more efficient organization of your application in Lithe.


This organization makes your application more scalable and easier to maintain, allowing you to focus on what really matters when developing on Lithe!

Release Statement This article is reproduced at: https://dev.to/lithephp/organizando-suas-rotas-de-forma-modular-e-automatica-no-lithe-4ohf?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3