Routing and Serving up static content with Zend Framework 1.10

Ever made a new controller and action and view, just to put some static content in the view?
Well lets sort it out and create a static controller which will route to any number of the view files in the controller:

zf create controller static index-action-included[=1] default

That puts it in our module’s controller. Open it up and delete the index action, then delete the index.phtml from the views folder.

Our new Action won’t have a set view file, instead it gets the path for it’s view file from the url, and will try to find a corresponding .phtml file.

<?php
class StaticController extends Zend_Controller_Action
{
   public function init()
   {
        /* Initialize action controller here */
   }

 public function displayAction()
 {
    //get the file name from the url
    $page = $this->getRequest()->getParam('page'); 

    //load the file in the view folder
    $file = $this->view->getScriptPath(null).'/'.$this->getRequest()->getControllerName().'/'.$page.'.'.$this->viewSuffix;

    //does the view file exist?
    if (file_exists($file)) 
    {
        //render the view
        $this->render($page);
        } 
        else 
        {
            //go to our error controller
            throw new Zend_Controller_Action_Exception('Page not found', 404); 
        }
    } 
}

Now create about.phtml in application/modules/default/views/scripts/static and enter some text.
Test it by going to http://site/static/display/page/about/

Now we can add other static .phtml files in the static folder like http://site/static/display/page/contact/, or http://site/static/display/page/privacy-policy/

However the url isn’t particularly great so we can use Zend Router in the bootstrap.

We’ll create  a function in the bootstrap. The modules bootstrap! It won’t run unless it’s a function called _initThis or _initThat, so I called mine _initRoutes.
And instead of extending Zend_Application_Bootstrap_Boostrap, we make it  Zend_Application_Module_Boostrap.
Also, it’ll flake out if there are two classes called Bootstrap, so even though the namespace for the default module isn’t required, we call the class Default_Bootstrap.
We tell the router to route any url starting with /content/somepage to go to the static controller and display action:

class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected function _initRoutes()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();

        //route for static content
        $router->addRoute(
        'content', new Zend_Controller_Router_Route('content/:page', array('controller' => 'static', 'action' => 'display'))
        );
    }
}

now browse to http://site/content/about, and you’ll see your static page. Awesome!

Also, since setting the project up as a modular application, we can safely delete the models, views, and controllers from the application folder, tidying things up even more.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s