phpcakephpcakephp-2.0

cake php link to the current page with different prefix


In my default layout I would like to show link which points to the current page but with different prefix. I am using prefix 'language' to use address like www.site.com/eng/controller/action/param.

I tried $this->Html->link('eng', array('language' => 'eng') );

But this creates link with url eng/controller/action without passed arguments, without named arguments and without url params.

How I can do this? I would prefer elegant solution like 1 line of code - I know it can be done but can't find it :(.


Solution

  • Try this:

    // helper method, possibly AppHelper, or in AppController and set a view var
    function getCurrentParams() {
        $route = Router::currentRoute();
        $params = $this->request->params;
        $pass = $params['pass'];
        $named = $params['named'];
        unset($params['pass'], $params['named']);
        if (!empty($route->options['pass'])) {
            $pass = array();
        }
        return array_merge($params, $named, $pass);
    }
    
    $params = $this->SomeHelper->getCurrentParams();
    $params['language'] = 'eng';
    // use $params for your link now