简体   繁体   中英

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 !

->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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM