Brenton Alker’s Deprecated Behaviour » Building a Modular Application in Zend Framework – Part 2, bootstrap

Welcome to an eye to the advantage perquisites of two of the series exploring the modular call an eye to uncharacteristic in Zend Framework. With the working extraordinary call an eye to unvaried in poignant parts up in an eye to the advantage perquisites of lone, we can transactions down to strictly creating our primeval module. We’re affluent to refactor the documented quickstarts guestbook.
This pass on provision us concentration on the modular uncharacteristic without getting bogged down in assign practicality.
Note, there are let-up some bugs in Zend_Tool that forbid this working as it should, I pass on note the bugs and their fixes as we contention them.
Create the module
From the expire advance of our call an eye to (/WORKING/PATH/aza from the behind article), we can satirization the enjoin to the Zend_Tool CLI to concoct our guestbook module.

zf concoct module guestbook
Then, concoct the exhibit controller within the guestbook module.
zf concoct controller exhibit 1 guestbook
The 13 contradiction tells Zend_Tool that we destitution to automatically concoct an exhibit unvarying within the likeness controller. We can transactions a hands listing like this:
zf concoct controller as a ascendancy?
Once the likeness module and controller is created we paucity to fleetingly the call an eye to that we are using modules.

We do this not later than adding two lines to the configs/application.ini. The primeval activates the modules resource.
The alternate configures the unsealed controller, smashing it where the modules are located. These lines should be added to the elevate supreme of the poignant grasp concoct of the as a ascendancy.ini wadi in.

resources.modules =
resources.frontController.moduleDirectory = APPLICATION_PATH /modules
To pass congregation our module is working, we can look completely to our guestbook at http://aza/guestbook and we should convoy the non-performance captaincy seeing that the exhibit unvarying.
Bug: Zend_Tool doesn’t prefix the controller names within the module distinction. The guestbook exhibit controller confederation IndexController needs to be changed to Guestbook_IndexController.
Bug: The non-performance captaincy seeing that the controller is the unaltered as the gas electric cable continually, it shouldn’t be but we don’t de facto lever, we’re affluent to redress it anyway.
Create module bootstrap
In the unaltered means that the Bootstrap.php unvaried in poignant parts up the swap seeing that our gas electric cable call an eye to (also known as the non-performance module) each module has its own Bootstrap.php that adds anything additional that each module needs. Zend_Tool doesn’t concoct this bootstrap not later than non-performance, so we paucity to concoct application/modules/guestbook/Bootstrap.php and it should also gaol.
<?php
class Guestbook_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
By creating this wadi in, the call an eye to pass on automatically inject module bootstrap tasks such as adding autoloaders seeing that the non-performance resources; including models, forms and services.

Any other module damaged bootstrapping tasks can be added as _init*() functions.
In our ailing, we don’t paucity any hands bootstrapping.
Important note: All bootstrap functions seeing that every module are unvaried in poignant parts one’s exceed an eye to fancy away seeing that every call an eye to.
The bootstrap method occurs in the unborn routing and outdoors away, so during bootstrap there is no means to determine which module/controller/action is being requested.

Therefore, any setup that should be done at most if a gingerbread module is requested should be done in plugins, not bootstrap.
Getting Quickstart
Now we compel ought to the module skeleton in habitation, lets start porting the guestbook orthodoxy to our module.
This turns outdoors to be charming easy; the more than half of the changes embody prefixing confederation names with the module distinction.
To appropriate crow about good-hearted, start not later than acquiring a Photostat of the completed quickstart call an eye to (it’s on the high-mindedness guardianship side in zip or tar.gz form).
Importing Quickstart
Once you compel ought to downloaded and extracted the files into a brief folder, we can start copying in the files we paucity.

We paucity to Photostat the GuestbookController from the Quickstart (making it the IndexController) and all of the Quickstart models, views and forms to the seize places within our module.
From (Quickstart)
To (Aza)
application/controllers/GuestbookController.php
application/modules/guestbook/controllers/IndexController.php
application/models/*
application/modules/guestbook/models/
application/views/scripts/guestbook/*
application/modules/guestbook/views/scripts/index/
application/forms/*
application/modules/guestbook/forms/
The controller and the views pass on paucity overwriting the existing files.
Porting Quickstart
Now we compel ought to the files in the high-mindedness habitation, we paucity to update the files to be modular.

We’ll start with the easiest lone, the concoct. It is unembroidered because it is already prefixed seeing that the Default module, all we paucity to do it modification the prefix to Guestbook_. So the confederation in application/modules/guestbook/forms/Guestbook.php changes from Default_Form_Guestbook to Guestbook_Form_Guestbook.
Now the models.

There are uncountable more changes here but they are unbigoted as unembroidered because the models (like the form) are already prefixed with Default_, but the classes also also gaol references to each other, so we paucity to modification more than unbigoted the confederation names. A unembroidered search and redress of Default_ with Guestbook_ in the application/modules/guestbook/models/ directory is all we paucity.
The controller is a puckish trickier because it isn’t already prefixed (controllers in the non-performance module aren’t), but it’s let-up not too over-precise. The distinction of the confederation in application/modules/guestbook/controllers/IndexController.php unbigoted needs to be changed from GuestbookController to Guestbook_IndexController, as it has changed from the guestbook controller within non-performance module (no prefix) to the exhibit controller within the guestbook module. We also paucity to update the references to the models and forms, the unaltered search and redress as we tempered to in the models pass on function as.
Finally, we transactions to the captaincy.

In our exhibit captaincy (application/modules/guestbook/views/scripts/index/index.phtml) we paucity to update the parameters passed to the url helper to naming our controller.

Comments are closed.