1

Ok, so I am using Zend Framework 1.12 with Doctrine 2.2.3. The Zend_Navigation class is too simple and static for my liking, so I am implementing my own navigation class.

The scenario is as follows:

My application requires the navigation menu to be made of standard (compulsory) items (such as "Home", "About Us" and others) as well as user specific items (depending on user role). The dynamic menu items are stored in a database (MySQL) and I use Doctrine as my ORM and DBAL.

I'm attempting to write my own navigation class and partial view to render its contents, and in doing so, would like to know what performance issues I will have, if I query the db on every action call, then send the menu items back to the page? What would the best way to implement caching be? I have started using Zend_Registry_Namespace to store my menu classes (the class also stores the current active menu item, which I use in my partial view to append CSS styling to the item).

Please help

ByteNudger
  • 1,545
  • 5
  • 29
  • 37
Blyde
  • 2,923
  • 2
  • 18
  • 16
  • Are you developing something CMS-ish, where users can add their own entries into the menu? Because, if not, you should not store this information in the DB at all! – markus Oct 09 '12 at 22:09
  • Hi there Markus, thanks for your respone. No it wouldn't reflect a CMS system. However the users navigation menu would dynamically generate depending on his/her role in the system. So an administrator would get the full menu, whereas a client role would only get certain menu items. – Blyde Oct 10 '12 at 06:48

1 Answers1

1

So, you're not developing a CMS where users can create their own menu structures and contents. In that case you should not store the menu information in the relational database at all. Querying data from a database always involves a lot of overhead compared to just reading the same information from a PHP array or a config file. You don't need to store anything in sessions either. I fact, it is really a simple case. Let the user log in, determine the user group/privileges of the user and programmatically piece together the menu.

You can prepare the different menu parts beforehand and then just merge the arrays and send the final array to the nav renderer.

Example:

Standard menu that everybody gets:

$navArray = array(
    array(
        'controller' => 'index',
        'label' => 'Home',
    ),
    array(
        'controller' => 'about',
        'label' => 'About',

    )
);

Then add entries depending on ACL roles:

if ($user->getRole() == 'administrator')
{
    $navArray[] = array(
        'controller' => 'tools',
        'label' => 'Tools'
    );
}
markus
  • 40,136
  • 23
  • 97
  • 142
  • Gee this is great feedback thank you. Another questions, the reason I'm using session is to store the menu state, is this necessary? For example, when the user navigates to a certain menu item, that item needs to change css class. What would the best solution be? – Blyde Oct 10 '12 at 13:21
  • Why does it need to change css class? – markus Oct 10 '12 at 13:47
  • Or rather add a new css class to show that he is on that particular menu. Give the menu item a background color (active) as apposed to no background (Inactive) – Blyde Oct 10 '12 at 14:42
  • That's a completely different topic and has nothing to do how you create the menu. – markus Oct 10 '12 at 19:01
  • Ok thank you for leading me in the right direction. Will mark this as accepted. It seems I'm over complicating things somewhat. Could you perhaps point me in the right direction on navigation menus and using CSS to style the active item? I'd like to stay away from Zend_Navigation if possible. – Blyde Oct 11 '12 at 09:52
  • Then just look at how Zend_Navigation deals with active state. Basically, it is the job of PHP to add the active css class to the active icon and it is the job of CSS to provide styles for this class. If you update your page without roundtrip, you will have to add the class with javascript. – markus Oct 11 '12 at 13:33
  • On another note I don't understand why you don't want to use Zend_Navigation. It's great in the sense that you can render page structures easily as menu, breakcrumb, sitemap, etc. and it's fully extendable. – markus Oct 11 '12 at 13:34
  • I have actually just been filtering through endless tutorials on Zend_Navigation. I'm starting to get the hang of it, but it seems to be quite a rigid helper. Especially when trying to render sub menus using your own HTML elements, like divs and positioning them in your own way. But i'll pursue on! – Blyde Oct 11 '12 at 17:14