Hi!
I'm new to Phalcon and I want to make a multi-lingual site with Phalcon. The URL structure is like this:
- https://example.com/about <-- for default language
- https://example.com/en/about <-- for English
I created a route group class for wrapping the routes and set prefix to URLs in the group so that I don't need to repeatly declare the routes.
The route group:
use Phalcon\Mvc\Router\Group;
class HomeRouteGroup extends Group
{
public function initialize($language = null)
{
if ($language === null || $language === 'hk') {
$language = 'hk'; // default language
} else {
$this->setPrefix('/' . $language);
}
$this->addGet('/', array(
'controller' => 'home',
'action' => 'index',
'language' => $language
))->setName('home');
$this->addGet('/test', array(
'controller' => 'home',
'action' => 'test',
'language' => $language
))->setName('test');
}
}
The routes.php:
<?php
$router = new \Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);
$router->mount(new HomeRouteGroup('hk'));
$router->mount(new HomeRouteGroup('en'));
$router->notFound(array(
'controller' => 'error',
'action' => 'error404',
));
return $router;
The HomeController:
class HomeController extends ControllerBase
{
public function indexAction()
{
echo 'home ' . $this->dispatcher->getParam('language') . '<br>';
echo $this->url->get('home') . '<br>';
echo $this->url->get('test');
}
public function testAction()
{
echo 'test ' . $this->dispatcher->getParam('language') . '<br>';
echo $this->url->get('home') . '<br>';
echo $this->url->get('test');
}
}
I found that for "example.com/" and "example.com/test", they works (going to the correct action, echo the correct language paramenter from dispatcher and echo the correct URL). But for "example.com/en", the router gave Error 404 and for "example.com/en/test", it can route to the testAction() but the $this->url->get() does not prepend the "/en/" segment.
How to make the router works for default path of "/en" and the URL generator include the "/en" segment?
Thanks!