在zend中实现动态侧边栏而不调用额外的操作

在zend中实现动态侧边栏而不调用额外的操作

问题描述:

Currently trying to come up with a "best practice" for a ZF site that is both elegant and efficient. What I am trying to do is just generate data for a sidebar.

In a couple of related questions and on other sources, the approach taken was to call another action from the sidebar part of your layout, or to add the sidebar action to an actionstack helper. These seemed very elegant, but add an extra request to the dispatch loop, which is very poor performance.

I've seen people suggest view helpers that do a lot more than just formatting... Which seems semantically pretty poor. Is there a way to automate the sidebar data generation before the layout gets to it?

目前正试图为优质高效的ZF网站提出“最佳实践”。 我想要做的只是为侧边栏生成数据。 p>

在几个相关问题和其他来源中,采取的方法是从您的侧边栏部分调用另一个操作 布局,或将侧边栏操作添加到动作堆栈助手。 这些似乎非常优雅,但是对调度循环添加了额外的请求,这是非常差的性能。 p>

我看到人们建议视图助手不仅仅是格式化。 这在语义上看起来很差。 有没有办法在布局到达之前自动生成侧边栏数据? p> div>

Hi there are a lot of different ways. I would use an FrontController Plugin wich sends the sidebar content to the layout script. Something like this:

/**
 * layout script
 */
<?php echo $this->sidebar; ?>

/**
 * Plugin
 */
class My_Plugin_Sidebar extends Zend_Controller_Plugin_Abstract {

    /**
     * preDispatch()
     *
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();

        $nav = new My_Model_Menu();
        $entries = $nav->getEntries();

        $html = '<h2> my sidebar </h2>';

        foreach ($entries as $e) {
            $html .= '<li>' . $e . '</li>';
        }

        $layout->sidebar = $html;

    }

}

If by sidebar you mean navigation, do check out the relatively new component Zend_Navigation which is shipped with a view helper.