Fanny
Fanny

Reputation: 127

How can I display/hide menu items with Zend Framework

[See update at the end]

I'm working with Zend framework, in PHP and I have some difficulties with Zend Navigation. It's my first question here, so if something's wrong with it, just tell me and I'll correct it.

I have a menu looking like this in my application

Home 
Login
Logout
Member's Page

I have a navigation xml file containing my menu.

<nav>
 <home>
    <label>Home</label>
    <uri>/</uri>
 </home>
 <login>
    <label>Login</label>
    <uri>/index/login</uri>
 </login>
 <logout>
    <label>Logout</label>
    <uri>/index/logout</uri>
 </logout>
 <member>
    <label>Member's Page</label>
    <uri>/index/member</uri>
 </member>
</nav>

Also a menu.phtml containing this

<div class="top-level">
<?php
foreach ($this->container as $page) {
    if ($page->isVisible()) {
        if ($page->isActive(true)) {
            if ($page->isActive(false)
                )$page->setClass("active");
            else
                $page->setClass("open");
            echo $this->navigation()->menu()->htmlify($page);
//... the same continue for the 3 menu level

Finally, in my layout.phtml, I have this to render the menu

<?php
      $partial = array('menu.phtml', 'default');
      $this->navigation()->menu()->setPartial($partial);
      echo $this->navigation()->menu()->render();
?>

For now, my menu works good, but I can't have Login and Logout always displayed in my menu. So, what I need to do, is to hide Login when I'm logged in, and to hide Logout when I'm logged out. It looked pretty simple when I started, and it still does, but I can't make it work. I don't know how and if I can hide and show item depending on logged users. I really need to make it work, because I will need to hide/display other items in the future.

So is there a way of doing that ?

Thanks !

EDIT :

I'm currently not using Zend::Auth or Zend_ACL for roles and authorization. If I want to know if the user is logged in or not, I have a token in the session that is valid only when the user is logged in. I'd like my menu to work without changing that if it's possible.

UPDATE :

I had it working in another way than those suggested. I'm really not sure it's a clean way, but it's doing the job for now. So now, my xml navigation file look like

<menuAnonymous>
     <home>
     <label>Login</label>
        <uri>/login</uri>
     </home>
    </menuAnonymous>
    <menuLogged>
     <home>
        <label>Logout</label>
        <uri>/Logout</uri>
     </home>
    </menuLogged>

I initialize both in my bootstrap like this.

$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'menuNotLogged');    
$container = new Zend_Navigation($config);
Zend_Registry::set('main',$container);

And then, in my layout, I look at my token and display the menu depending on that.

if ($tokenValid) {
     echo $this->navigation()->menu()->render(Zend_Registry::get('main')); 
} else {
     echo $this->navigation()->menu()->render(Zend_Registry::get('logged'));
}

So it's working like I want now, but I still want to do it cleaner, so if you have any suggestions to help me ... thank you !

Upvotes: 3

Views: 3002

Answers (2)

Fanny
Fanny

Reputation: 127

I updated my question with the solution I used, maybe I'll find a better way to do it later but for now this is it. I didn't find the solution here, that's why I'm answering my own question. Thanks for your help !

Upvotes: 1

wimvds
wimvds

Reputation: 12850

You should have a look at the "Leveraging Zend_Navigator" webinar on http://www.zend.com/en/resources/webinars/framework. It explains how you can tie your navigation to specifiec roles/ACL.

Upvotes: 1

Related Questions