phpdatabasezend-frameworkcontent-management-systemzend-layout

Load Zend Layout from a Database


I am building a cms on top of Zend, just to practice and for fun. I would like to be able to store the layout scripts and view scripts in the database, and retrieve them from there, so they are easily editable from within my CMS. Could someone point me in the right direction? What I do now is this:

// 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');
        }

But this obviously means I lose all the advantages of Zend in rega


Solution

  • I think a better approche would be to have your CMS code write a versioned layout script on each change to file. Then set the appropriate layout script for the application from the database.

    I would still store all the code in the database for backup purposes and to load for editing, but write it out to file when you are finished editing it.

    Layout Database Table

    | id | layout | version | filename | content |
    

    When you save to this table. Write the content to a file in application/layout/[layout]-[version].phtml

    Then use this pseudocode in your bootstrap to load the page you created in your CMS.

    Bootstrap.php

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

    This way you can keep all the server side scripting inside the layout file and make use of the placeholders component rather than str_replace