Pureandfast
Pureandfast

Reputation: 29

Slim framework dynamic routes

I am working with Slim Framework. I would like to make dynamic routes so that my index.php files is not full of static routes.

Instead of having this that works :

$app->get('/mypage', function() use ($app) {
    $app->render('mypage.php', compact('app'));
})->name('mypage');

I would like to have something like this (that does not work) :

$app->get('/:name', function($name) use ($app) {
    $app->render('template.php', compact('app', 'name'));
})->name(:name);

thanks for you help !

Upvotes: 1

Views: 2809

Answers (1)

Lutsen
Lutsen

Reputation: 153

->name(:name) assigns a name to the route. The name has to be a string. But you don't need this to create a dynamic route, you can just write

$app->get('/:name', function($name) use ($app) {
    $app->render('template.php', compact('app', 'name'));
})

More on route names:

Update: The above is an answer for the V2 version of the Slim framework. More on naming routes in Slim V3 here: https://www.slimframework.com/docs/objects/router.html#route-names

Upvotes: 1

Related Questions