views:

307

answers:

2

I have a database driven navigation mainly composed of two tables: menus and a menu_items, this works out fine for purely "static" links but now I need to have a dynamic link ( login/logout ).

My menu_items table is just composed of links to pages manually added in the admin. So now I need to adjust the table and model possibly such that it can handle "dynamic" links.

Here's the schema for the menu_items table:

CREATE TABLE `menu_items` (
  `id` int(11) NOT NULL auto_increment,
  `menu_id` int(11) default NULL,
  `label` varchar(250) default NULL,
  `page_id` int(11) default NULL,
  `link` varchar(250) default NULL,
  `position` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8

Menu id corresponds to another table, so 1 for example is the primary nav, 2 can be utility nav. Position refers to the order relative to each menu in which the menu item is displayed. Link is just a field, eg '/user/login'.

I'm looping through each of my menus and creating Navigation containers and using Zend Navigation for this.

Here's logic I'm using for my login area, I'll need to move this over and incorporate it into the dynamic navigation:

<?php if($this->identity == null) { ?>
    <p><a href='/user/login'>Login</a></p>
<?php }else{ ?>
    <p>Welcome back <?php echo $this->identity->first_name;?></p>
    <p>To log out <a href='/user/logout'>click here</a></p>
<?php } ?>

It looks like I need to add a new column for access level for each of the menu items, then update my Menu model to account for whether the user is logged in or not and query the menu item's new access column value, or something along those lines. Anyone have any suggestions?

+2  A: 

I guess you could achieve this with Zend_Acl. All built-in Navigation helpers add integration with ACL and translation.

From http://framework.zend.com/manual/en/zend.view.helpers.html

{get|set}Acl(), {get|set}Role(), gets/sets ACL (Zend_Acl) instance and role (String or Zend_Acl_Role_Interface) used for filtering out pages when rendering, and {get|set}UseAcl() controls whether ACL should be enabled. The methods hasAcl() and hasRole() checks if the helper has an ACL instance or a role registered.

If a logged-in visitor has a different role than a not-logged-in visitor (user vs guest), this should work. I've never had the need to do that before, so I cannot give you any code snippets, but check http://www.youtube.com/watch?v=n31mQGZxtbE for a video tutorial on how to combine both.

I still don't get why you want to refactor this though, as moving it seems to be a hassle and adds unneccessary complexity for something that could live happily in a separate View Helper. So, why not keep it simple :)

Gordon
Because it would be more flexible. If say, I wanted this link in the middle of say, 4 links, that is so it appears third from the left out of 5 links total instead of at the end, or in a separate region.
meder
Well, it's your app, so do as you like. I just think that's a lot of refactoring for an IF. If yagni then yagni :) Anyway, let me know if it works with Zend_Acl (if you are taking that road)
Gordon
+2  A: 

Gordon's ACL is the way to go (and I upvoted it). I just wanted to pipe in and describe exactly what I do.

I created both the sign in and sign out pages in my navigation config:

    <user_signin>
      <label>Sign in</label>
      <other_stuff></other_stuff>
      <resource>mvc:user_signin</resource>
      <privilege>navigate</privilege>
    </user_signin>

    <user_signout>
      <label>Sign out</label>
      <other_stuff></other_stuff>
      <resource>mvc:user_signout</resource>
      <privilege>navigate</privilege>
    </user_signout>

Then, in my ACL:

// 'mvc:home' is wide open, 'user' role inherits from 'guest'
$this->addResource(new Zend_Acl_Resource('mvc:user_signin'), 'mvc:home');
$this->deny('user', 'mvc:user_signin', 'navigate');

$this->addResource(new Zend_Acl_Resource('mvc:user_signout'), 'mvc:home');
$this->deny('guest', 'mvc:user_signout', 'navigate');
$this->allow('user', 'mvc:user_signout', 'navigate');

That way, it's just another piece of navigation, and behaves like the rest, just with a little logic behind it.

Derek Illchuk
So you rely purely on xml for the navigation?
meder
Yes. My navigation config contains *every* page, each having a unique `resource` value. Then, each is shown or not based on my ACL.
Derek Illchuk