Search code examples
phpzend-frameworkcachingdoctrine-ormzend-navigation

Should my Zend navigation menu be explicitly cached?


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


Solution

  • 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'
        );
    }