Search code examples
phpzend-frameworkzend-db

Menu and all pages content from database


I'm writing simple website with some cms functions. It would be good to have all menu items and pages contents held in database.

I have table with id, name, parent_id and a content field. In future I would maybe move content to a content table to have multiple contents to menu item with fk. But it is not the case here.

The question is:

  1. Do I need the URL field in menu table?
  2. What else do i need to get it to work? Should every page have its own controller? I,m a beginner with zend framework, so please give me some directives. Thanks in advance.

Solution

  • Lets start with Question 2: every page does not need its own controller. If your pages are static you can even load every page using a single action. For more dynamic processing you could use a separate action for each page.

    In any case, make sure you structure your code into controllers and actions in a way that makes sense. For example, inside your CMS a user might edit, create or delete a post. You could then create a PostController inside which you write an editAction, createAction and deleteAction.

    You could store the URL in the table, but you do not necessarily have to.

    Single action approach (mostly for static content)

    Make sure the page id or name is stored in a GET param. You could then use the following code:

    public function genericpageAction()
    {
        $thePageID = $this->_request->getParam('id');
    
        // fetch the page content from the db based on $thePageID
        // and pass it to the view
    }
    

    Of course, here, you could also match against the URL stored in the table if you chose that approach.

    Multiple action approach (for more dynamic processing, most likely what you want with a CMS)

    You could define a route for each page and load its content in the respective action. For example, for the page to edit a post:

    class MyCMS_PostController extends Zend_Controller_Action
    {
        public function editAction()
        {
            // fetch the home page content
            // do any further processing if necessary
        }
    }