a problem with Zend Framework .

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guosheng1987
Forum Newbie
Posts: 24
Joined: Thu Oct 15, 2009 3:03 am

a problem with Zend Framework .

Post by guosheng1987 »

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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: a problem with Zend Framework .

Post by Eran »

Inside the view you have a helper that renders URLs.

Code: Select all

echo $this -> url();
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
guosheng1987
Forum Newbie
Posts: 24
Joined: Thu Oct 15, 2009 3:03 am

Re: a problem with Zend Framework .

Post by guosheng1987 »

pytrin wrote:Inside the view you have a helper that renders URLs.

Code: Select all

echo $this -> url();
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

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 ;
        
    }
 
Is this a good way to do it ,or you can get another better idea?
Thanks
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: a problem with Zend Framework .

Post by Eran »

I do something similar, though you do some unnecessary operations.
$moduleName = Zend_Registry::get("moduleName");
Why are you setting/getting those options in the registry? they are all available in the request object.

Code: Select all

$request = $this->getRequest();
$homeUrl = ($request -> getModule() != 'default' ? ('/' . $request -> getModuleName()) : '') 
      . '/' . $request -> getControllerName() . '/' . $request -> getActionName();
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
Post Reply