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

Redirect multilang

Hello guys. I have a problem with router and stuck a bit. Have a site (single page) multilang. If some try to open mysite.com/blah/ to be redirect in default language wich is "en" Here is my router:

$router = new \Phalcon\Mvc\Router(false);
$router->add('/{language:[a-z]{2}}/:controller', ['controller' => 2, 'action' => 'index', 'language' => 1]);

and when i try to redirect or forward with dispacher occur "Dispatcher has detected a cyclic routing"

Here is my controller and my beforeExecuteRoute

public function indexAction() {
        echo 'blah';
    }

    public function beforeExecuteRoute(Dispatcher $dispatcher) {
        $lang = $this->request->get('_url');
        $lang = ($lang != '/en/' && $lang != '/de/') ? '/de/' : $lang;
        $this->dispatcher->forward(array(
            "controller" => "index",
            "action" => "index",
            "language" => $lang
        ));
        return false;
    }

I've try to init this in initialize function with $this->response->redirect(), but the result was the same

The beforeExecuteRoute event is executed before each request. So $this->dispatcher->forward() is forwarding again and again. You could check if the forward was already done with wasForwarded():

public function beforeExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
    if(!$this->dispatcher->wasForwarded()) {
        $lang = $this->request->get('_url');
        $lang = ($lang != '/en/' && $lang != '/bg/') ? '/bg/' : $lang;
        $this->dispatcher->forward(array(
            "controller" => "index",
            "action" => "index",
            "language" => $lang
        ));
        return false;
    }
}


1.5k

Thank you for reply, error disappear but dispacher doesnt work at all. Again i've tried to change some array elements in forward but... without success

What URL are you accessing ? and what do you expect to happen ?