Hi guys .
now I am building my blog with Zend Framework 1.9
when I display my view,I want to get the page url without any parameters.
like http://myblog/admin/article/index/
$homeUrl = "/admin/article/index/";
where and when should I get it ?
in route ends? or in the method preDispatch()?
I need it in each of my actions...
and in every them, it's totally different.
a problem with Zend Framework .
Moderator: General Moderators
-
guosheng1987
- Forum Newbie
- Posts: 24
- Joined: Thu Oct 15, 2009 3:03 am
Re: a problem with Zend Framework .
Inside the view you have a helper that renders URLs.
Without any parameters it will render the current URL. you can pass parameters in an array to create modified URLs in the same manner.
http://framework.zend.com/manual/en/zen ... lpers.html
Code: Select all
echo $this -> url();http://framework.zend.com/manual/en/zen ... lpers.html
-
guosheng1987
- Forum Newbie
- Posts: 24
- Joined: Thu Oct 15, 2009 3:03 am
Re: a problem with Zend Framework .
pytrin wrote:Inside the view you have a helper that renders URLs.Without any parameters it will render the current URL. you can pass parameters in an array to create modified URLs in the same manner.Code: Select all
echo $this -> url();
http://framework.zend.com/manual/en/zen ... lpers.html
I am sorry but maybe you misunderstand me .
I know use " echo $this->view->url(); " can get the recent url ,but I have to strip the papameters again and again
like http://localhost/admin/index/index/pg/1
what I want is http://localhost/admin/index/index
now I deal with it like this.
Code: Select all
public function preDispatch()
{
$this->setHomeUrl();
$this->view->assign("homeUrl",$this->getHomeUrl());
}
public function setHomeUrl()
{
$request = $this->getRequest();
$moduleName = Zend_Registry::get("moduleName");
$controllerName = Zend_Registry::get("controllerName");
$actionName = Zend_Registry::get("actionName");
if("default" == trim($moduleName))
$this->_homeUrl = $request->getBaseUrl(). '/'. $controllerName .'/'.$actionName ;
else
$this->_homeUrl = $request->getBaseUrl(). '/'. $moduleName. '/' .$controllerName .'/'. $actionName ;
}
Thanks
Re: a problem with Zend Framework .
I do something similar, though you do some unnecessary operations.
Also, you should never modify the view in the preDispatch hook. This would mean that a view would exist for every request - which isn't necessarily true (not every action returns output). I'm assuming you are using the viewRenderer() since you don't call initView()
You should be implementing this in the initView() method instead
Why are you setting/getting those options in the registry? they are all available in the request object.$moduleName = Zend_Registry::get("moduleName");
Code: Select all
$request = $this->getRequest();
$homeUrl = ($request -> getModule() != 'default' ? ('/' . $request -> getModuleName()) : '')
. '/' . $request -> getControllerName() . '/' . $request -> getActionName();You should be implementing this in the initView() method instead