Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Slim Framework nested routes

Tags:

php

routes

slim

i try something like:

    $app = new Slim();

    $app->get("/home", function() use($app) {

       //some query for sub pages
       $my_sub_page = 'subpage';

       $app->get("/home/" . $my_sub_page, function() use($app) {

           //

       });

});

but the result of www.site.com/home/subpage is 404... is possible to do something like this? I miss something?

Thanks.

like image 560
Ste Avatar asked Jan 17 '26 08:01

Ste


2 Answers

Use http://docs.slimframework.com/#Route-Groups

<?php
$app = new \Slim\Slim();

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

    // Library group
    $app->group('/library', function () use ($app) {

        // Get book with ID
        $app->get('/books/:id', function ($id) {

        });

        // Update book with ID
        $app->put('/books/:id', function ($id) {

        });

        // Delete book with ID
        $app->delete('/books/:id', function ($id) {

        });

    });

});
like image 136
jsmarkus Avatar answered Jan 19 '26 22:01

jsmarkus


You probably want to use route parameters:

$app->get("/home/:mysubpage", function($mysubpage) use($app) {
    //do something with $mysubpage
    //it contains the value of www.site.com/home/{whatever-you-put-here}
});

This helps you to get dynamic routes with arbitrary arguments.

like image 36
Odi Avatar answered Jan 19 '26 22:01

Odi