[英]Modular web site with zend framework, stack of actions
如何使用 Zend 框架構建模塊化 web 站點。 我在數據庫中有頁面,每個頁面都表示為 url。 每個頁面都有 1toN 的內容。 每個內容都有 controller、action 和 position(+其他現在不重要的列)。 因此,一個請求是一個頁面和多個內容(多個操作)。 如何在 output 之前構建所有操作? 我想要像下面的示例這樣的布局設計,其中內容放在容器中(操作在布局打印輸出之前運行)。
<div id="left">
<?= $this->layout()->left_container ?>
</div>
<div id="center">
<?= $this->layout()->center_container ?>
</div>
<div id="right">
<?= $this->layout()->right_container ?>
</div>
到目前為止,我從布局視圖中調用了操作,但我不喜歡這種方法:
foreach ($contents as $item) {
echo $this->action($item['action'], $item['controller'], null, array('content' => $item));
}
謝謝。
ps
adepretis 的代碼與我的類似,我的操作視圖在布局內運行,這意味着當發生錯誤時,它會打印在調用操作的布局中。 在布局 output 之前構建操作是否沒有乳清? 另一個不好的事情是,在我必須運行的每個操作中...->setResponseSegment,我希望它是自動化的。
附言 #2
我找到了答案,它列在下面作為答案。 如果有乳清我可以更容易地做到這一點,請寫下來。
您可以使用ActionStack 助手。 例如:
class MyController_Action extends Zend_Controller_Action {
function init() {
/** you might not want to add to the stack if it's a XmlHttpRequest */
if(!$this->getRequest()->isXmlHttpRequest()) {
$this->_helper->actionStack('left', 'somecontroller', 'somemodule');
$this->_helper->actionStack('center', 'somecontroller', 'somemodule');
$this->_helper->actionStack('right', 'somecontroller', 'somemodule');
}
}
class MyController extends MyController_Action {
function indexAction() {
// do something
}
}
class SomecontrollerController extends MyController_Action {
function leftAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('left_container');
}
function centerAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('center_container');
}
function rightAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('right_container');
}
}
對/somemodule/my/index的請求會導致執行/somemodule/somecontroller/left 。 /somemodule/somecontroller/right , /somemodule/somecontroller/center最終出現在對應的布局段中。
我在其他論壇上找到了我的答案。 這是答案:
我的插件
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$action_stack = new Zend_Controller_Action_Helper_ActionStack();
// here I will read actions from db and run it in loop, but for example few are staticly added bellow
$action_stack->actionToStack('index', 'content', 'default', array('position' => 'left'));
$action_stack->actionToStack('index', 'content', 'default', array('position' => 'center'));
$action_stack->actionToStack('index', 'edo', 'default', array('position' => 'center'));
$action_stack->actionToStack('left', 'edo', 'default', array('position' => 'left'));
$action_stack->actionToStack('right', 'edo', 'default', array('position' => 'right'));
}
}
BaseController ,每個 controller 擴展
class BaseController extends Zend_Controller_Action
{
public function preDispatch()
{
$position = $this->_request->getParam('position', false);
if ($position) {
$this->_helper->viewRenderer->setResponseSegment($position);
}
}
}
布局.phtml
<div>
<h2><u>LEFT:</u></h2>
<?=$this->layout()->left?>
</div>
<div>
<h2><u>CENTER:</u></h2>
<?=$this->layout()->center?>
</div>
<div>
<h2><u>RIGHT:</u></h2>
<?=$this->layout()->right?>
</div>
這就是我想要的,如果有人有更好的解決方案,請回答這個問題,我會接受他的回答。
你好我也遇到同樣的問題。 您建議的解決方案工作正常。 但是我的baseController在模塊base中。代碼與baseController一起工作順利但是當我在另一個模塊中擴展控制器時發生錯誤,因為base Controller無法在其他controller中識別例如:modules/base/Controller/baseController modules/user/Controller/userController
任何解決方案?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.