How To Create Multiple Themes Application With Zend Framework?

Zend Framework is a good choice for developers who want a well-designed, flexible PHP framework.

I’ve been using PHP because of its speed and flexibility, which are weaknesses of other scripting languages.

With phpBB, WordPress… or almost other PHP softwares, the users can have various themes to switch between them easily. Zend Framework does not natively support this feature but we can do it using 2 solutions.

The first, as many developers have been using, is that you can use Zend_Layout to create multiple layouts. This sounds like ASP.NET’s Master Page.

You can follow this guide to do it yourself: http://framework.zend.com/manual/en/zend.layout.html

Recently I’ve found out a simple solution that can thoroughly solve our problem. We’ll need to create a “bridge controller”, this controller’s objective is to change the View’s script path.

class BridgeController extends Zend_Controller_Action
{
    public function init()
    {
        $this->view->setScriptPath('/path/to/your/view/script/directory');
        parent::init();
    }
}

For example, your current directory structure is like below:

app/

|—modules/

|—|—default/

|—|—|—controllers/

|—|—|—views/

|—|—|—|—helpers/

|—|—|—|—scripts/

You may add another scripts directory as a sub-directory of views, in this tutorial I name it scripts2:

app/

|—modules/

|—|—default/

|—|—|—controllers/

|—|—|—views/

|—|—|—|—helpers/

|—|—|—|—scripts/

|—|—|—|—scripts2/

Now, our bridge controller’s init method will be:

public function init()
{
    $this->view->setScriptPath('/path/to/app/modules/default/views/scripts2');
    parent::init();
}

After that, we must change our controllers’ parent classes from the old ones to BridgeController. For example, class IndexController extends Zend_Controller_Action must be changed to class IndexController extends BridgeController.

For the class to be auto-loaded, you can put the BridgeController file into the same directory with Zend library:

lib/

|—Zend/

|—BridgeController.php

If wanting to switch to another theme, we just change ’scripts2′ to its name in the init method, or we can change it dynamically based on cookies, URL parameters…

affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “How To Create Multiple Themes Application With Zend Framework?”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>