Zend Framework 中的视图重载

Zend Framework 中的视图重载

问题描述:

假设我有 2 个控制器,内容和新闻:

Let's say I have 2 controllers, content and news:

class ContentController 扩展 Zend_Controller_Action { }

class ContentController extends Zend_Controller_Action { }

class NewsController 扩展 ContentController { }

class NewsController extends ContentController { }

如果没有找到新闻控制器的视图,我希望 Zend 使用其父控制器的脚本路径.如何在无需路由到其父控制器的情况下实现此目的?

If there are no views found for the news controller, I want Zend to use the scripts path of its parent controller. How can I achieve this without having to route to its parent controller?

您必须手动添加 scriptPath:

You will have to add the scriptPath manually:

class ContentController extends Zend_Controller_Action { 

   public function init()
   {
      $this->view->addScriptPath(APPLICATION_PATH . '/views/scripts/content');
   }

}

class NewsController extends ContentController {

   public function init ()
   {
       // Ensure that ContentController inits.
       parent::init();
      $this->view->addScriptPath(APPLICATION_PATH . '/views/scripts/news');
   }
}

这将使用视图脚本变形器的堆栈功能.它将首先查看最后指定的路径,即 APPLICATION_PATH .'/views/scripts/news',如果在那里找不到脚本,它将在堆栈上的第二个目录中查找,即 APPLICATION_PATH .'/views/scripts/content'.

This will use the stack functionality of the view script inflector. It will first look in the path last specified, which is APPLICATION_PATH . '/views/scripts/news', if the script isn't found there, it will look in the second directory on the stack, which is APPLICATION_PATH . '/views/scripts/content'.