views:

60

answers:

2

I'm using an XML config file to populate my navigation using Zend_Navigation.

I have Login and Logout in the navigation, but obviously I should only display the 1 action that makes sense.

I believe I can do something like $navigation->removePage() but... How do I get the $navigation variable in a Controller after it was previously created in Bootstrap.php?

A: 

If you are using Zend_Application, setup Zend_Navigation as a resource (see the link on how to set your pages in your configuration file )

http://framework.zend.com/manual/en/zend.application.available-resources.html

Then you can access the navigation resource in a controller like so:

 class FooController extends Zend_Controller_Action
  {
      public function init()
      {
          $bootstrap = $this->getInvokeArg('bootstrap');
          $navigation = $bootstrap->getResource('navigation');
          // ...
      }
  }
Travis
I was able to get the nav in my IndexController from $this->view->Navigation(); ... but I'm still unsure how to associate the Login and Logout actions with Zend_Acl appropriately, so that only Login is available at first and then only Logout is available once the user has logged in.
jwhat
+2  A: 

The easiest way is to create ACL with apropriate privilages for logged and not logged users, then in config file:

resources.navigation.pages.login.resource = "user"
resources.navigation.pages.login.privilege = "login"

(this is the ini format for simplicity, you may do this in XML as well)

Privilages will limit displaying login/logout links for specified group.

However… This is good for static navigation labels. I'd like to have login link named: Login, and logout named: Logout (+ username), so the user sees his identity all the time.

In this case I'd create a front controller plugin which retrieves actual navigation container, finds login page container and replaces label and route (or URI or module,controller and action if you are using MVC page containers).

Updated:

Try something like this:

if (Zend_Auth::getInstance()->hasIdentity()) {
    Zend_Registry::set('role',
    Zend_Auth::getInstance()->getStorage()->read()->role);
} else {
    Zend_Registry::set('role', 'guest');
}

$this->_acl = new My_Model::Acl;
$this->_auth = Zend_Auth::getInstance();

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Plugin_AccessCheck($this->_acl));

$view->navigation()->setAcl($this->_acl)->setRole(Zend_Registry:get('role'));
takeshin
I added <resource>user</resource> and <privilege>logout</privilege> to the navigation.xml config file, but I'm not sure how to tie the rest of this together.I have my Zend_Acl code in the init() method of IndexController.php (not even sure if that's the appropriate place). Can you please share the code required to accomplish this?
jwhat
-- edited my original post --
takeshin
Perfect! I put that code in a _initAcl() method in Bootstrap where I configured my addRole(), addResource(), allow() and deny() accordingly. Thanks!
jwhat