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

language in url and translated routes

hi

i want to have my routes follow this format www.domain.name/language/controller/action/etc so for english we have www.domain.name/en/products/list/etc for italian we have www.domain.name/it/prodotti/listino/etc if i call www.domain.name/it/products/list/etc should not work

i'm not sure how to explain better

is this kind of thing possible with phalcon ? how would you do this ?

thanks in advance for any help



98.9k
Accepted
answer

You can create your routes that way:

<?php

$router = new Phalcon\Mvc\Router(false);

$router->add('/{language:[a-z]+}/:controller/:action', array(
    'controller' => 2,
    'action' => 3
));

$router->add('/{language:[a-z]+}/:controller/:action/:params', array(
    'controller' => 2,
    'action' => 3,
    'params' => 4
));

https://docs.phalcon.io/en/latest/reference/routing.html#usage-examples

thanks i will try and come back if i have problems

I know that this discussion is rather old, but I've the same problem that quasiperfect had. The answer from phalcon reports exactly what I had done in my project, but there is one thing I did not understand: how do you translate the url without duplicating controllers and actions for each language?

I have followed also this discussion: forum.phalcon.io/discussion/1527/multilingual-routing I am not sure, however, if that is the best answer for me; it use a redirection, but I want to use only one request. Is there in Phalcon a tool to do that?

Thank you very much.