DEV Community

Cover image for Creating route directory in Slim Php Framework
Asif Sheikh
Asif Sheikh

Posted on

Creating route directory in Slim Php Framework

Hello Everyone,

I have discussed on creating route directory in slim php framework. It makes easy to organize or modular your slim project.

Let's go,

STEP 1:
Firstly, copy your code in public/index.php:

$routes = glob(__DIR__ . "/../routes/*.php");

foreach($routes as $route)
{
     (require $route)($app);
}
Enter fullscreen mode Exit fullscreen mode

STEP 2:
In second step, we'll create routes directory in our slim project directory and also create routes/web.php file:
Like this,

<?php

use Slim\App;

return function (App $app)
{
     $app->get("/your-route", function ($request, $response, $args){
           $response->getBody()->write("Hello, I am in your route");
           return $respones;
     });
};
Enter fullscreen mode Exit fullscreen mode

I hope you to enjoy for creating such routes separate files that may be slim code organizable, readable.

Thank you for giving your valuable time to read this!✨

Top comments (0)