Search code examples
phpzend-frameworkzend-navigation

zend headtitle using zend_navigation


I am using zend_navigation for creating menu. Which works fine. Now i m looking if i can take current page and set it as page title using headTitle. Is there is any way to do that?

or

how can i use config files (.ini) for creating Pagetitle and meta data ?


Solution

  • In controller you can get current active page and get its's label. Then you cen set it as page title.

    //get active page and its label
    $activePage = $this->view->navigation()->findOneBy('active', true);
    $label = $activePage->get('label');
    
    //set page label as html title
    $this->view->headTitle($label);
    

    You could also write custom plugin to do it for you in every request:

    class Plugin_NavigationTitle extends Zend_Controller_Plugin_Abstract
    {
        function preDispatch(Zend_Controller_Request_Abstract $request)
        {
            //get view
            $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
    
            //get active page and its label
            $activePage = $view->navigation()->findOneBy('active', true);
            $label = $activePage->get('label');
    
            //set page label as html title
            $view->headTitle($label);
        }
    }