Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top