douyanjing8287 2009-11-30 02:03
浏览 58
已采纳

在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?

  • 写回答

2条回答 默认 最新

  • dongmi1872 2009-11-30 07:55
    关注

    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;
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度