Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate Silex routes

I'm trying to make urls translatable in my Silex app.

First, I tried overriding UrlGenerator and RedirectableUrlMatcher, but that didn't really work.

Then, I tried overriding:

$app['route_class'] = 'My\Translatable\Route';

with code like this:

class Route extends Silex\Route
{
    public function setPattern($pattern)
    {
        return parent::setPattern(str_replace('admin', 'admin2', $pattern));
    }
}

But I'm getting https://gist.github.com/6c60ef4b2d8d6584eaa7.

What is the right way to achieve this?

like image 827
umpirsky Avatar asked Nov 12 '22 20:11

umpirsky


1 Answers

So the solution is to extend RedirectableUrlMatcher and overwrite match method instead of Route.

Matcher.php

class Matcher extends Silex\RedirectableUrlMatcher
{
    public function match($pathInfo)
    {
        return parent::match(str_replace('/admin', '/', $pathInfo));
    }
}

app.php

$app['url_matcher'] = $app->share(function () use ($app) {
    return new Matcher($app['routes'], $app['request_context']);
});

Now when I'm accessing http://domain.com/admin silex returns content for http://domain.com/. Hope this is what you need.

like image 178
Norbert Orzechowicz Avatar answered Nov 15 '22 11:11

Norbert Orzechowicz