开发者

Is it possible to get current url with zend url helper reseting parameters and not passing urlOptions?

If I call $this->url() from a view I get the url with parameters. Ex: /test/view/var1/value1

Is there any way to get the currect url/location witho开发者_Go百科ut parameters (var1/value1) and without passing the urlOptions:

For example, if I use this it works:

$this->url(array("controller"=>"test", "action"=>"view"),null,true); //Returns /test/view

But I would like to avoid passing the parameters

$this->url(array(),null,true);

Is there any way to do this?


This sounds like a job for a view helper. Something like this?

class Zend_View_Helper_Shorturl {

    public function shorturl() {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $module = $request->getModuleName();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        return $this->view->url(array('module'=>$module, 'controller'=>$controller,'action'=>$action), null, true);
        //return "/$controller/$action"; //Left this in incase it works better for you.

    }
}  

Then you just write $this->shorturl(); in your view.

Just to be clear this would go in scripts/helpers/Shorturl.php

Edit: In fact, I've just tried this and it works. I'd say this is the solution to use.

class Zend_View_Helper_Shorturl {

    public function shorturl() {
        return $this->view->url(array('module'=>$module, 'controller'=>$controller,'action'=>$action), null, true);
    }
}


Not really - the Zend_Controller_Request_Http object that the router (called by the url view helper) uses to generate the link doesn't really distinguish between the module/controller/action parameters and other parameters your action might use.

Either use the first form that you quoted above, or if you need a solution that works for every action/controller, create something like:

class MyApplication_Controller_Action_Base extends Zend_Controller_Action {
    public function preDispatch() {
        //Generate a URL to the module/controller/action 
        //(without any other parameters)
        $this->view->bareUrl = $this->view->url(
            array_intersect_key(
                $this->getRequest()->getParams(),
                array_flip(array('module','view','controller')
            ),
            null,
            true
        );
    }
}

In any view, you can then use <?=$this->bareUrl?>


Just use $this->url() to get the baseUrl/controller like it appears in the browser URL for example if your it is www.your-domain.ro/project/members it will return project/members

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜