We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multilingual routing combine

I want to combine multi router at once. Following example, i have 2 router each language for one page.

For english search page:

$router->add('/search', array(
    'controller' => 'search',
    'action' => 'index'
))->setName('search-en');

For turkish search page:

$router->add('/{lang:[a-z]{2}}/arama', array(
    'controller' => 'search',
    'action' => 'index',
))->setName('search-tr');

I have to seperate to call for each language.

For english link:

$this->url->get(['for' => 'search-en']); // /search

For Turkish link:

$this->url->get(['for' => 'search-tr']); // /arama

I'm using dynamic vars for temp solution:

$lang = defaultLang();
$this->url->get(['for' => "search-{$lang}"]);

I was expecting single router for each language (could be with array) and will be usage:

 $this->url->get(['for' => 'search']); // will generate url matched

Any idea or solution about that?

Thanks

Well how about extending url and get method ? So if wou will call get("search") it will add language to this key and call parent get url ?



58.1k

I'm not pro, could you share more detail about the solution please? Thanks

Well how about extending url and get method ? So if wou will call get("search") it will add language to this key and call parent get url ?

edited Sep '16
class MyUrl extends Phalcon\Mvc\Url
{
    public function get($uri = null, $args = null, $local = null, $baseUri = null) {
        $lang = // get language from somwhere
        $name = "$uri-$lang";
        $params = array_merge (['for' => $name], $args);
        return parent::get($params)
    }
}

Also i don't quite understand why there is lang parameter just in turkish route ? Then you need to deal with it too. Using what i wrote above you can just do:

$this->url->get('search');

And you will get proper link for current language where your $lang is given from somewhere like request, or whatever other solution. Also second parameters in this guys will be some params you want to generate for route, like id or something.



58.1k

Multilingual url is important for the SEO. My urls are like that:

Root is english:

/

/search

/skills

/web-design

Turkish lang urls:

/tr

/tr/arama

/tr/beceriler

/tr/web-tasarim



58.1k

I already use own get method, i want to combine multilingual routers for same page. I dont want to create new router for each language. Is there any great solution or my solution that i have already use is right?

What you mean you don't want to create new router for each language ? You only need one router object, no need for router for each language :)



58.1k

Exactly what i want ;))

What you mean you don't want to create new router for each language ? You only need one router object, no need for router for each language :)

Then just have one router and that's it. Add all routes to it and no problem :)