从数据库加载 Zend 布局



我正在Zend之上构建一个cms,只是为了练习和娱乐。我希望能够在数据库中存储布局脚本和查看脚本,并从那里检索它们,以便可以从我的 CMS 中轻松编辑它们。有人可以指出我正确的方向吗?我现在做的是:

// Disable view
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
    $pageDB = new Application_Model_DbTable_Page();
    $page = $pageDB->fetch($identifier);
         // Display the page or a 404 error
        if ($page !== null) {
            $this->view->headTitle($page->title);
            // Get the layout from the DB
            $layoutDB = new Application_Model_DbTable_Layout();
            $layout = $layoutDB->fetch($page->layout);
            $layout = str_replace('{LCMS:title}', $page->title, $layout->content);
            $layout = str_replace('{LCMS:content}', $page->content, $layout);
            $this->getResponse()->setBody($layout);
        } else {
            $this->_forward('notfound', 'error');
        }

但这显然意味着我在rega中失去了Zend的所有优势

我认为更好的方法是让您的 CMS 代码在每次更改文件时编写版本化的布局脚本。然后从数据库中为应用程序设置适当的布局脚本。

我仍然会将所有代码存储在数据库中以进行备份和加载以进行编辑,但是当您完成编辑后将其写出到文件中。

布局数据库表

| id | layout | version | filename | content |
  • 布局具有页面的标识符。
  • 版本是一个自动增量器,每次更改都会更新
  • 文件名为 [布局]-[版本]
  • 内容
  • 就是内容...

保存到此表时。将内容写入应用程序/布局/[布局]-[版本].phtml中的文件

然后在引导程序中使用此伪代码加载您在 CMS 中创建的页面。

引导.php

public function _initLayout() {
    $layoutDB = new Application_Model_DbTable_Layout();
    $layout = $layoutDB->fetch($page->layout);
    Zend_Layout::getMvcInstance()->setLayout($layout->filename);
}

这样,您可以将所有服务器端脚本保留在布局文件中,并使用占位符组件而不是str_replace

最新更新