Handling Static Files in Zend Framework

There are a couple of alternatives when you need to render static files in your Zend Framework MVC application:

1. Completely bypass the framework

The easiest solution is to move such content into a subfolder in the “public” directory. The default .htaccess file already setup to serve any physical file that is web-accessible. For example, http://localhost/ZFApp/public/static/myfile.html would display myfile.html contained in the “static” subfolder.

Advantage: minimum overhead imposed by the framework

Disadvantage: You lose out on all the great features provided by the framework such as Caching, Layouts, Error Handling and Security – This approach is therefore not recommended and is reserved for the simplest of apps.

2. Create a “custom route” specifically to handle static pages:

Add the following route definitions to your application.ini

resources.router.routes.staticpage.route = /statics/:filename
resources.router.routes.staticpage.defaults.controller =
staticcontent
resources.router.routes.staticpage.defaults.action = display

This staticpage custom route defined above will take a url of the form “/statics/about”, store “about” as a parameter named “filename”, open a controller named “staticcontentController” and execute an action named “displayAction” in it. The displayAction can then render the file about.phtml (stored in views/scripts/staticcontent)like so:

class StaticcontentController extends Zend_Controller_Action
{
// action to handle ALL static pages
public function displayAction()
{
$page = $this->getRequest()->getParam('filename');
$this->render($page);
}
}

3. Empty action stubs : The ViewRenderer (builtin) helper automatically renders foo/foo.phtml after executing “fooAction”. So just creating empty action blocks, and copying the static content into the desired .phtml file would accomplish what we want. On the downside, your code may get  peppered with empty actions over time (not that there is anything inherently wrong with this.. I resort to doing this for the occasional static file).

4. Overload the php magic function “__call” to intercept calls to non-extant actions and render them. This technique allows you to totally omit defining empty action stubs and relies on the php magic method to hande them.

class StaticcontentController extends Zend_Controller_Action 
{
public function __call($method, $args) 
{
if ('Action' == substr($method, -6)) 
{
$action = $this->getRequest()->getActionName();
return $this->render($actionName);
}
}
}

Now, your controllers (with lots of statics) should extend StaticcontentController (instead of Zend_Controlller_Action). When the framework tries to invoke aboutAction, it will fail to find the method, get trapped by the __call method and then render the about.phtml page.

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