当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How do I define multiple routes for one request in Silex?

问题描述:

Is there a way in Silex to define multiple routes for one request. I need to be able to define two routes for one page (both routes takes to the same page). Here's my current controller:

$app->get('/digital-agency', function() use ($app) {

return $app['twig']->render('digital_agency.html', $data);

});

It works when I duplicate the function like this:

$app->get('/digital-agency', function() use ($app) {

return $app['twig']->render('digital_agency.html', $data);

});

$app->get('/agencia-digital', function() use ($app) {

return $app['twig']->render('digital_agency.html', $data);

});

So, any idea of a more clean way to do it?

网友答案:

You can save the closure to a variable and pass that to both routes:

$digital_agency = function() use ($app) {
    return $app['twig']->render('digital_agency.html', $data);
};

$app->get('/digital-agency', $digital_agency);
$app->get('/agencia-digital', $digital_agency);
网友答案:

You can also use the assert method to match certain expressions.

$app->get('/{section}', function() use ($app) {
    return $app['twig']->render('digital_agency.html', $data);
})
->assert('section', 'digital-agency|agencia-digital');
分享给朋友:
您可能感兴趣的文章:
随机阅读: