views:

269

answers:

2

Hi,

i´m a little bit confused by reading all this posts an tutorials about staring with Zend, because there a so many different ways to solve a problem.

I only need a feedback about my code to know if iam on the right way:

To simply get a (hard coded) Navigation for my side (depending on who is logged in) i build a Controller Plugin with a postDispatch method that holds following code:

 public function postDispatch(Zend_Controller_Request_Abstract $request) 
 {
  $menu = new Menu();

  //Render menu in menu.phtml
  $view = new Zend_View();

  //NEW view -> add View Helper
  $prefix = 'My_View_Helper';
  $dir = dirname(__FILE__).'/../../View/Helper/';
  $view->addHelperPath($dir,$prefix);

  $view->setScriptPath('../application/default/views/scripts/menu');
  $view->menu = $menu->getMenu();

  $this->getResponse()->insert('menu',  $view->render('menu.phtml'));     

 } 

Is it right that i need to set the helper path once again? I did this in a Plugin Controller named ViewSetup. There i do some setup for the view like doctype, headlinks, helper paths...(This step is from the book: Zend Framework in Action)

The Menu class which is initiated looks like this:

class Menu 
{
 protected $_menu = array();

 /**
 * Menu for notloggedin and logged in
 */
 public function getMenu()
 {
  $auth = Zend_Auth::getInstance();
  $view = new Zend_View();

  //check if user is logged in
  if(!$auth->hasIdentity()) {
   $this->_menu = array(
    'page1' => array(
     'label' => 'page1',
     'title' => 'page1',
     'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page1'))
    ),
    'page2' => array(
     'label' => 'page2',
     'title' => 'page2',
     'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page2'))
    ),
    'page3' => array(
     'label' => 'page3',
     'title' => 'page3',
     'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page3'))
    ),
    'page4' => array(
     'label' => 'page4',
     'title' => 'page4',
     'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page4'))
    ),
    'page5' => array(
     'label' => 'page5',
     'title' => 'page5',
     'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page5'))
    )
   );
  } else {
   //user is vom type 'client'
   //..

  }

  return $this->_menu;
 }
}

Here´s my view script:

    <ul id="mainmenu">
 <?php echo $this->partialLoop('menuItem.phtml',$this->menu) ?>
</ul>

This is working so far. My question is: is it usual to do it this way, is there anything to improve? I´m new to Zend and in the web are many deprecated tutorials which often is not obvious. Even the book is already deprecated where the autoloader is mentioned.

Many thanks in advance

A: 

You shouldn't be creating a new view. Since you have already created the View object in your Boostrap (and used it to render the rest of the site) you should fetch the already created view object.

If you are using Zend_Application_Resource to setup your view in the bootstrap you can fetch it like this:

$view = Zend_Controller_Front::getInstance()
    ->getParam('bootstrap')
    ->getResource('view');

This way there is no need to set the view helper path again and create another view object.

If you are not using the Zend_Application to boostrap your app you could try something like this:

$view = Zend_Layout::getMvcInstance()->getView();
Goran Jurić
Woks with the second solution!Many thanks! I love this website here. Faster response than in forums.Would be nice if you can tell me the difference between this two cases.Is there a recommended solution?
Oliver
I wouldn't say that there is a recommended solution. It depends on your application setup.
Goran Jurić
A: 

Unless you are working on a relatively small side, I wouldn't do this in the controller, since you will have to add this to many controllers. Why not check in the bootstrap or even checking in your layout would make more sense to me although it wouldn't be proper.

TaMeR