Change default Base Url

Hi there, I'm struggling with this for some hours already. I have a multilingual site by subfolders, so URL/es/ for spanish, URL for english(default), etc... The problem are the redirections or the normal hrefs/forms, doesn't catch the url/language/ as a base like the ajax calls.(So if i click in login href in mysite/es goes to mysite/login and not mysite/es/login).After tried everything, nothing works but the cleanest solution would be to change the base URL of the site for the /language/ version but I just cannot! I can change it for an specific controller but not for all system dinamically. I've tried accesing by new DI, a factorydefault one but no luck. How can I change the default Base URL on demand? I've even tried with PHP plain code but still I cannot. If that isnt as well the proper way please enlight me, coz I'm really blocked. The routers are properly configured, I've tried choosing the language as a namespace as well but then the routers doesnt work...

EDIT: Ok, i figure out something but I don't think is the best way, I set the language param as a variable in the view and I add $language to the URLs and the redirections. I wait for a better solution anyway.

just post your code i will check whats the best solution for you

edited Mar '15

just post your code i will check whats the best solution for you

Ok, routing is like this type, just for languages available. If not available or is english I just rewrite to plain url(without /([a-z]{2}) subfolder) :

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

The <a> tags are typical ones with an standard href but if I am in localhost/es and I press LogIn, I want to go to localhost/es/login and not localhost/login.

<a href="login" title='Login'>Log In</a>

Also happend the same if I do a redirect in the controller.

$this->response->redirect('/login');

I've used the phalcon boostrap so I just set up in config.pho the base URL(ie. in local):

'baseUri'        => 'http://localhost',

So the problem is that base Uri, it always erase the language subfolder from the base URL, even if I do:

    $di = new Phalcon\DI\FactoryDefault();
    $di->set('url', function (){
        $url = new Phalcon\Mvc\Url();
        $url->setBaseUri('http://localhost/'.$language);
        return $url;
    }, true);
or
    $this->di->set('url', function (){
        $url = new Phalcon\Mvc\Url();
        $url->setBaseUri('http://localhost/'.$language);
        return $url;
    }, true);

As much it just work for the actuall controller, for new calls it doesnt(I've tried with setshared as well). I've found few workarounds and for now seems the best:

 public function beforeExecuteRoute(){
    $language=$this->dispatcher->getParam("language");
     $this->di->set('url', function (){
        $url = new Phalcon\Mvc\Url();
        $url->setBaseUri('http://localhost/'.$language);
        return $url;
    }, true);
 }

or

$language='/'.$this->dispatcher->getParam("language"); + preappend $language to every call and redirection.

The best would be to catch that language subfolder in the config(or services) php files and set that modified baseUri but I just don't find the way.

edited Mar '15

Ok, I've managed a bit better solution(cleaner). I have this code on every controller:

    public function beforeExecuteRoute()
    {
        $language=$this->dispatcher->getParam("language");
        if($language !== null) $this->url->setBaseUri($this->url->getBaseUri().'/'.$language);
    }

Like that redirections works perfect, plain hrefs no, but with this for example href={{ url('/login') }} does it.

its ok but just try also the link_to from view

I feel comfortable with plain html. Also allows me to compress and minimize with automatic tools.

its ok but just try also the link_to from view