Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

symfony

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?

like image 929
arielcr Avatar asked Mar 10 '14 20:03

arielcr


2 Answers

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);
like image 156
Maerlyn Avatar answered Oct 14 '22 14:10

Maerlyn


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');
like image 23
dsoms Avatar answered Oct 14 '22 14:10

dsoms