1
votes

I might be approaching this the wrong way so I am open to alternatives.

I would simply like to match the following sample urls using a single route:

/

/welcome
/en/welcome
/fr/welcome

/my/arbitrarily/deep/path
/en/my/arbitrarily/deep/path
/fr/my/arbitrarily/deep/path

etc

Here is what I have so far:

$app->get('/{_locale}{path}', function (Request $request) use ($app) {
    $path = $request->attributes->get('path');
    // do stuff with path here
})
->value('_locale', 'en')
->assert('_locale','^(en|fr)?$')
->value('path', 'index')
->assert('path', '.*')
->bind('*');

Now this seems to work as expected, but when I try to use the twig path() or url() it fails to build the correct url, for example:#

on /foo (no locale specified on the url so defaults to en):

{{ path('*', {path:'foo/bar'}) }}

will result correctly in

foo/bar

on /fr/foo, the same call:

{{ path('*', {path:'foo/bar'}) }}

results in

frfoo/bar

This is because of the missing / between {_locale} and {path}, but by changing the route to:

/{_locale}/{path}

It stops matching /foo and only matches either /en/foo, /fr/foo or //foo.

I'm not sure where to go from here :s

I didn't want to use multiple routes (maybe one with and without a {_locale}) because I'm not sure how that works with the path() function, I basically want the result of path() to include the current locale in the url if it's not 'en' (I think is what I'm getting at).

Can anyone help me with this?

Cheers

Toby

1

1 Answers

-1
votes

Declare a route for /{_locale}/{path} and /{path}.

As for path() , since you defined a default _locale value for the first route , there should not be any issues in your views.