2009-12-05 98 views

回答

1

您可以創建一個前端控制器插件叫做LayoutDefaults:

class MyLib_Controller_Plugin_LayoutDefaults extends Zend_Controller_Plugin_Abstract 
{  
    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
    $mvc = Zend_Layout::getMvcInstance(); 
    if (!$mvc) return; 
    $view = $mvc->getView(); 
    if (!$view) return; 

    /** 
    * Set the defaults. 
    */ 
    $view->value1 = "default value1"; 
    } 
} 

在前端控制器:

Zend_Controller_Front::getInstance() 
    ->registerPlugin(new MyLib_Controller_Plugin_LayoutDefaults()); 

在你layout.phtml:

<?= $this->escape($this->value1) ?> 

而且最後,在您的控制器中,根據需要覆蓋默認設置:

$this->view->value1 = "new value 1"; 
0

這聽起來像你試圖保持視圖內容的控制器。我也相信試圖將視圖內容保留在控制器之外,所以我儘可能將視圖內容放入視圖中。我做這種方式:

例如,在layout.phtml我可能有兩個佔位符,一個爲標題,另一個是主要內容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title><?php echo $this->escape($this->placeholder('titleContent')) ?></title> 
</head> 
<body> 
    <div id="maincontent"> 
    <?php echo $this->layout()->content ?> 
    </div> 
</body> 

然後在index.phtml視圖本身我把這兩個內容如下:

<?php $this->placeholder('titleContent')->Set('My Index Page Title') ?> 
<p>Index page content here</p> 

您可以添加任意數量的佔位符,而不會對控制器造成任何影響。使用這種方法,大多數內容都不在控制器中,除非它來自模型。

1

創建新的抽象控制器,將延伸Zend_Controller_Action

IndexController extends My_Controller_Action - >My_Controller_Action extends Zend_Controller_Action

還有,你應該把在任何你想要的init()。 :)

+0

好吧,認爲這會工作,我會試試=)! – user199337 2009-12-05 16:14:56