2016-12-16 95 views

回答

1

可以佈局之間爲特定的控制器的作用,通過使用下面的代碼切換:

// A controller's action method that uses an alternative 
// layout template. 
public function indexAction() 
{ 
    //... 

    // Use the Layout plugin to access the ViewModel 
    // object associated with layout template. 
    $this->layout()->setTemplate('layout/layout2'); 

    //... 
} 

除了Layout控制器插件,有Layout視圖 助手提供相同的功能。使用佈局視圖 幫助程序,您可以(例如)從沒有特定控制器操作的「靜態」頁面 切換佈局。

設置佈局的控制器的所有操作

如果一個控制器類的所有操作方法都需要使用相同的替代佈局,則可以覆蓋AbstractActionController類的onDispatch()方法,並調用setTemplate()方法有,如下面的例子所示:

// Add this alias in the beginning of the controller file 
use Zend\Mvc\MvcEvent; 

// ... 

class IndexController extends AbstractActionController 
{ 
    /** 
    * We override the parent class' onDispatch() method to 
    * set an alternative layout for all actions in this controller. 
    */ 
    public function onDispatch(MvcEvent $e) 
    { 
    // Call the base class' onDispatch() first and grab the response 
    $response = parent::onDispatch($e);   

    // Set alternative layout 
    $this->layout()->setTemplate('layout/layout2');     

    // Return the response 
    return $response; 
    } 
} 

Reference

1

Zend Framework - Issue

>您可能要改變基於當前 模塊上的佈局。這需要(a)檢測在 路由中匹配的控制器是否屬於該模塊,然後(b)更改查看模型的模板 。

做這些動作的地方是在監聽器中。它應該監聽 以低(負)優先級的「路由」事件,或在任何優先級的「調度」事件中聽取 。通常,您將在引導事件期間註冊此 。

namespace Content; 
 

 
class Module 
 
{ 
 
    /** 
 
    * @param \Zend\Mvc\MvcEvent $e The MvcEvent instance 
 
    * @return void 
 
    */ 
 
    public function onBootstrap($e) 
 
    { 
 
     // Register a dispatch event 
 
     $app = $e->getParam('application'); 
 
     $app->getEventManager()->attach('dispatch', array($this, 'setLayout')); 
 
    } 
 

 
    /** 
 
    * @param \Zend\Mvc\MvcEvent $e The MvcEvent instance 
 
    * @return void 
 
    */ 
 
    public function setLayout($e) 
 
    { 
 
     $matches = $e->getRouteMatch(); 
 
     $controller = $matches->getParam('controller'); 
 
     if (false === strpos($controller, __NAMESPACE__)) { 
 
      // not a controller from this module 
 
      return; 
 
     } 
 

 
     // Set the layout template 
 
     $viewModel = $e->getViewModel(); 
 
     $viewModel->setTemplate('content/layout'); 
 
    } 
 
}

手冊上說以上,但如果你想使用這些代碼,你將需要:

// module/Content/config/module.config.php 
 

 
return [ 
 

 
    /* whatever else */ 
 

 
    'view_manager' => [ 
 
     'template_map' => [ 
 
      'content/layout' => __DIR__ . '/../view/layout/layout.phtml'
 
 
     ] 
 
    ] 
 
];

很快,當所有模塊成功初始化(引導)時,Zend將自動調用onBootstrap(),這會將'dispatch'事件綁定到setLayout()方法,其中控制器名稱與當前模塊的名稱空間匹配,如果成功,則使用setTemplate()來設置佈局模板。

例如(!成功)

Module/Namespace: Content,

Controller: Content\Controller\MatchMeController,

Controller: Other\Controller\DontMatchMeController,(失敗!)

但是有一個小小的缺點:setLayout()使用

strpos(controller, __NAMESPACE__) === false

確定當前模塊,但如果我在其他某個模塊中有ContentController會怎麼樣?因此,使用

strpos(controller, __NAMESPACE__) !== 0

代替。

----------

Zend Framework - Issue

該手冊相當詳細,它也提到許多其他像對不同的控制器(或動作)設置不同的佈局。

相關問題