Setting up for a Modular Directory Structure
Using a modular directory structure allows code to be separated into self-contained units. It can be thought of as independent smaller MVC applications within your main application. For instance, a website may have a blog and admin module besides the default module. When creating a new project by using the Zend Framework CLI Tool, a modular structure is not present by default. In this article we will show how to change this by changing the project’s configuration file.
Editing the Configuration File
The first thing to do is to change the application/configs/application.ini file so that the framework knows to use a modular structure and where to find the modules. In the [production] section, add the two lines below.
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
Creating the Required Directories
Now we need to create the required directories. First, we need to create the directory which will contain our modules; modules as per our configuration file. Create the following two directories:
application/modules
application/modules/default
Now we have created the directory for the default module, but we need to add folders within it to contain models, views and controllers. If you used the Zend Framework CLI Tool to create your project, you can simply cut and paste the models, views and controllers folders that were automatically created within the application folder. Otherwise you will have to create the folders manually like discussed in the section below.
Creating the Directories By Hand
If you are creating your project by hand, then create the below folders.
application/modules/default/models
application/modules/default/views
application/modules/default/views/scripts
application/modules/default/views/scripts/index
application/modules/default/controllers
If you manually created the above folders, then you need to create a controller and a view script. Create the file application/modules/default/controllers/IndexController.php and fill it with the code below.
class IndexController extends Zend_Controller_Action {
public function indexAction() {
}
}
The indexAction will automatically look for the view script application/modules/default/views/scripts/index/index.phtml, so create that file with your desired HTML content. Also note that for exceptions (e.g. incorrect URLs), you should consider creating an ErrorController along with a view script to handle these gracefully.
Testing the Modular Application
Now that everything is ready, it is time to see the modular application in action. Try to visit your website’s front page. Entering localhost (or wherever your site is located) into your browser’s address bar should show you the front page. Behind the scenes, the router is automatically routing the request to your-domain/default/index/index. Enter this in your address bar and you should see the same page.
Congratulations, you are now ready to develop modular applications in the Zend Framework!
Here is what you will learn:
- Understand the theory of Zend Framework in details
- How to implement an enterprise-ready architecture
- Develop professional applications in Zend Framework
- Proficiently work with databases in Zend Framework
- ... and much more!