How to create a route with dash and underscore in silex
Question
i've got a project in php-silex, but i beging with this framework. I create some route but some doesn't work. Here some exemple:
/*
**INIT HERE & SOME OTHER CODE
*/
$app->match('letter-{letter}', function($letter) use()
{
echo 'With dash';
});
$app->match('letter_{letter}', function($letter) use()
{
echo 'With underscore';
});
$app->match('{other}', function($other) use()
{
echo 'Other view';
});
If i enter /letter-a the output is 'Width dash' but if i enter '/letter_a' the output is 'Other view' and not 'Width underscore'.
I want to known why this happened and how to show 'Width underscore' ?
Solution
The problem is that the routes: letter-{letter}
and letter_{letter}
resolve to the same route name, thus the second one overrides the first. The solution is to explicitly give the routes a unique name.
You can do that by using bind
:
$app->match('letter-{letter}', function ($letter) {
echo 'With dash';
})
->bind('letter_dash');
$app->match('letter_{letter}', function ($letter) {
echo 'With underscore';
})
->bind('letter_underscore');
I'll open a ticket on the silex tracker to see if we can give an error when two routes with the same name exist. (EDIT: done)
OTHER TIPS
I don't believe they allow that type of syntax, they are doing whild card lookups between your slashes'/' and taking the entire value, obviously they are not allowing true pattern matching.