DEV Community

Lithe
Lithe

Posted on

2 1

Organizing Your Routes Modularly and Automatically in Lithe

Organizing routes in an application can be challenging, especially as the number of pages increases. With this in mind, Lithe offers a solution to organize your routes in a cleaner, more modular way using the set('routes', ...) method. It simplifies the route registration process, allowing you to focus on the logic of your application while the system handles the rest automatically.

How It Works

When you use set('routes', ...) in Lithe, the system automatically locates and loads all PHP files within the specified routes folder (including subfolders). Each route file is 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.

Be Careful When Using Subfolders

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

However, if you use subfolders like panel/index.php, the system will not map it to the /panel route but to /panel/index. To ensure the route is correctly mapped to /panel, you should name the file as panel.php instead, like this:

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

This approach helps avoid route conflicts and makes the file structure clearer and more intuitive.

Directory Structure

Here’s an example of how the directory structure can look:

/routes
    cart.php
    checkout.php
    /admin
        dashboard.php
        users.php
Enter fullscreen mode Exit fullscreen mode

Defining Routes in Files

In each route file, you can use the coding style you prefer, whether functional syntax or classic syntax.

Example of cart.php:

get('/', function ($req, $res) { 
  $res->send('Cart'); 
});
Enter fullscreen mode Exit fullscreen mode

Example of admin/dashboard.php:

$router->get('/', function ($req, $res) { 
  $res->send('Admin Dashboard'); 
});
Enter fullscreen mode Exit fullscreen mode

Application Configuration

To define 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 the path and load routes automatically
Enter fullscreen mode Exit fullscreen mode

With this configuration, the system will automatically locate and load all defined routes, simplifying the process and ensuring a 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 truly matters when developing in Lithe!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
okumarkets profile image
Oku Markets

Cool!

Collapse
 
lithephp profile image
Lithe

Glad you think so! 😄 You can check out more about Lithe and all its features on the official Lithe site.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay