arielcr
arielcr

Reputation: 1663

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?

Upvotes: 7

Views: 2681

Answers (2)

dsoms
dsoms

Reputation: 155

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

Upvotes: 10

Maerlyn
Maerlyn

Reputation: 34107

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

Upvotes: 15

Related Questions