1
votes

How can I categorise routes in Slim Framework?

I have these basic/ front routes in my public dir,

$app->get('/about', function () use ($app, $log) {
    echo "<h1>About ", $app->config('name'), "</h1>";
});

$app->get('/admin', function () use ($app, $log) {
    echo 'admin area';
    require dirname(__FILE__) . '/../src/Admin/index.php';
});

// To generic
$app->get('/', function () use ($app, $log) {
    echo '<h1>Welcome to ', $app->config('name'), '</h1>';
});

// Important: run the app ;)
$app->run();

As you can see that when the route is on /admin, it will load the index.php in Admin directory.

Admin/index.php,

    // API group
    $app->group('/admin', function () use ($app) {

        // Contact group
        $app->group('/contact', function () use ($app) {

            // Get contact with ID
            $app->get('/contacts', function () {
                echo 'list of contacts';
            });

            // Get contact with ID
            $app->get('/contacts/:id', function ($id) {
                echo 'get contact of ' . $id;
            });

            // Update contact with ID
            $app->put('/contacts/:id', function ($id) {
                echo 'update contact of ' . $id;
            });

            // Delete contact with ID
            $app->delete('/contacts/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

        });

        // Article group
        $app->group('/article', function () use ($app) {

            // Get article with ID
            $app->get('/articles', function () {
                echo 'list of articles';
            });

            // Get article with ID
            $app->get('/articles/:id', function ($id) {
                echo 'get article of ' . $id;
            });

            // Update article with ID
            $app->put('/articles/:id', function ($id) {
                echo 'update article of ' . $id;
            });

            // Delete article with ID
            $app->delete('/articles/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

        });

    });

Let's say I have article, contact, etc modules in my admin area. In each of these modules I have routes for get, put, delete. So I group them by module as in Admin/index.php. But I get 404 page not found, when I request these modules on my url, for instance http://{localhost}/admin/contact I should get list of articles on my browser but I get a 404.

I don't have this problem if I put the grouped routes in public/index.php but I don't want to clutter this index as in time I have more modules to add in. I prefer splitting the routes into different indexes.

So is it possible to split the grouped routes into different index.php in different locations (directories).

Or maybe this is not how I should do it in Slim? if so, what is Slim's way for solving this problem?

1
You have not defined GET route for /admin/contact List of contacts in your code is GET /admin/contact/contacts - Mika Tuupola
I put them in a different index file and include the file. but i guess this is not how it works in slim.... - Run
It is possible can define routes in separate files. However you have not defined the GET /admin/contact route at all which is why you get 404. - Mika Tuupola
sorry I don't get it. can you please show me some examples? thanks. - Run

1 Answers

1
votes

In your code /admin/contact is the group for the routes that handle the contacts' GET/PUT/DELETE

// see here v 
    $app->group('/contact', function () use ($app) {}):

If you want to have a route with this URL segment then you should replace group with get

maybe you can remove the groups within the admin

 // API group
    $app->group('/admin', function () use ($app) {

        // Contact group
    //  $app->group('/contact', function () use ($app) {

            // Get All Contacts
            $app->get('/contacts', function () {
                echo 'list of contacts';
            });

            // Get contact with ID
            $app->get('/contacts/:id', function ($id) {
                echo 'get contact of ' . $id;
            });

            // Update contact with ID
            $app->put('/contacts/:id', function ($id) {
                echo 'update contact of ' . $id;
            });

            // Delete contact with ID
            $app->delete('/contacts/:id', function ($id) {
                echo 'delete contact of ' . $id;
            });

     // });
    });

Then you can access the routes as follow

Get All -> admin/contacts
Get One -> admin/contacts/:id
Update ->  admin/contacts/:id