Sunday, April 15, 2012

Setting Up Modular Application Structure In Zend Framework.


I would be taking time out to write blog posts on Zend Framework and I hope to share as much as I can.

I started using Zend framework a couple of months back and it gets the job done nicely. The only pain I had when I picked up the framework was the relative steep learning curve (steep when compared to things like Kohana and Code Igniter which I have played with in the past). And the steep learning curve is not as a result of the framework being inherently difficult to use but due to the apparent paucity of up to date and useful resources and guides out there. Truth be told, the level of documentation has improved in recent times, but I feel a framework as matured as Zend deserves more.

My ensuing posts on Zend Framework won’t touch on the basics, i.e. setting up the framework; understanding MVC, how controllers work etc. No, the things I would write about would be a little bit above the basics. The ideal audience would be someone who has managed to set up a working copy of the framework but still find concepts like Auto loading, Resource plugins, View Helpers etc. little bit fuzzy.

Looking for setup articles? You would find a nice one on Nettut. The quick start guide on Zend.com is also a good read.

So that been said, let’s get to business.

The very first thing I would blog about is something I feel you should take into consideration right after you have mastered the set up process of Zend Framework and you are able to set up your project. And this is Directory structure your application would take.

Before we get into the details, let’s talk about what I mean by Directory Structure in the context of Zend Framework. It is simply the way your files are arranged in directories in the file system.

There are different ways you can have your files laid out on the file system for your application and the way you choose would go a long way in determining how your application is structured and managed.   The recommended structure (my personal recommendation and a couple of others) would be the Modular structure.

As you must probably know, if you use Zend_Tool  to create your project the default layout would look thus:

/app
    application/
        configs/
        controllers/
     ErrorController.php
              IndexController.php
        views/
             helpers/
             scripts/
                 error/
                      error.phtml
                 index/
                      index.phtml

        forms/
    docs/
    library/
    public/
        css/
        js/
        images/
        uploads/


In this set up, there is one general Controllers, Views, and Models folder where all the respective controllers, views and models files would be found. This set up is workable for a relatively small project, but when it comes to a much larger project the modular directory structure is most of the time advised.

And what is the Modular Directory Structure? It looks like this.


/app
    application/
        configs/
        modules/
             default/
                   controllers/
                   layouts/
                   views/
                   forms/
             admin/
                   controllers/
                   layouts/
                   views/
                   forms/
             profile/
                   controllers/
                   layouts/
                   views/
                   forms/
    search/
                   controllers/
                   layouts/
                   views/
                   forms/
    docs/
    library/
    public/
        css/
        js/
        images/
        uploads/


In this set up, your models, views, and controllers are grouped together into self-contained “boxes” based on the different functional areas of your Web application. So for example if you have a web application that has the following functionalities/sections Admin, profile, search. You can easily package the files (controllers, views, models and more) responsible for these sections into distinct boxes (actually directory). Having such helps in having an easily decoupled and manageable set up where new functionality or third party component can easily be plugged in into your application.


So how do we create a modular directory structure in Zend?

There are two ways. You can either do it with the Zend_Tool or create it manually. It is recommended you use the Zend_Tool. Creating it manually is just having to create the files and directories the Zend_Tool would have created for you automatically.

Note that to use the Zend_Tool you need to set it up. Find instructions on how to do this here

To create a modular application structure using Zend_Tool, issue the following command:


# mkdir /var/www/app
# cd /var/www/app
# zf create project .
Creating project at /var/www/app


Now change into the app directory and issue the following command, to create the admin module

zf create module admin

The following operations would be performed


Creating the following module and artifacts:
/var/www/app/application/modules/admin/controllers
/var/www/app/application/modules/admin/models
/var/www/app/application/modules/admin/views
/var/www/app/application/modules/admin/views/scripts
/var/www/app/application/modules/admin/views/helpers
/var/www/app/application/modules/admin/views/filters
Updating project profile '/var/www/app/.zfproject.xml'


Now as you can see you have your respective ‘controllers’, ‘models’, views/scripts, views/helpers, views/filters created. All you now need to do is to populate them with respective .php and .phtml files.

But we are not finished yet. Final thing to do in other to have our Zend Framework Module aware is to edit the application.ini file appropriately. To do so, just add the following lines to the end of the Production block.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

resources.modules[] =


One last thing that you might want to do is to have autoloading functionality within your module. To do this, just create a Bootstrap.php file inside your module directory, in this case admin and have the following codes contained in it


class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{


public function _initload()
{

//your code goes here
}
}


And you have everything set up.

To access your admin module, navigate to the url: http://hostname/admin/

No comments: