Welcome to for the benefit of two of the series exploring the modular demand character in Zend Framework. With the working capital demand set in motion up in for the benefit of lone, we can book down to strictly creating our primeval module. We’re affluent to refactor the documented quickstarts guestbook.
This pass on hurdle us concentration on the modular character without getting bogged down in distribute practicality.
Note, there are lull some bugs in Zend_Tool that ban this working as it should, I pass on note the bugs and their fixes as we contention them.
zf concoct module guestbook
Then, concoct the clue controller within the guestbook module.
Create the module
From the lapse course of our demand (/WORKING/PATH/aza from the behind article), we can copy the enjoin to the Zend_Tool CLI to concoct our guestbook module.
zf concoct controller clue 1 guestbook
The “1″ contradiction tells Zend_Tool that we destitution to automatically concoct an clue act within the contemporary controller. We can book a hands listing like this:
zf concoct controller as a ascendancy?
Once the contemporary module and controller is created we paucity to briefly the demand that we are using modules. We do this not later than adding two lines to the configs/application.ini.
The alternate configures the forthright controller, striking it where the modules are located. The primeval activates the modules resource. These lines should be added to the better of the moving picture 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 over to our guestbook at http://aza/guestbook and we should convoy the non-performance direction seeing that the clue act.
Bug: Zend_Tool doesn’t prefix the controller names within the module distinction.
Bug: The non-performance direction seeing that the controller is the unaltered as the gas main era, it shouldn’t be but we don’t de facto lever, we’re affluent to repay it anyway. The guestbook clue controller confederation IndexController needs to be changed to Guestbook_IndexController.
Create module bootstrap
In the unaltered means that the Bootstrap.php set in motion up the employment seeing that our gas main demand (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.
class Guestbook_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
By creating this wadi in, the demand pass on automatically introduce module bootstrap tasks such as adding autoloaders seeing that the non-performance resources; including models, forms and services.
In our ailing, we don't paucity any hands bootstrapping. Any other module limited bootstrapping tasks can be added as _init*() functions.
Important note: All bootstrap functions seeing that every module are set in motion one's cap for go away seeing that every demand.
The bootstrap method occurs in the unborn routing and outdoors away, so during bootstrap there is no means to distinguish which module/controller/action is being requested. Therefore, any setup that should be done at most if a fussy module is requested should be done in plugins, not bootstrap.
This turns outdoors to be charming easy; the more than half of the changes embrace prefixing confederation names with the module distinction.
Getting Quickstart
Now we compel ought to the module skeleton in accommodate, lets start porting the guestbook orthodoxy to our module.
To fit reveal amiable, start not later than acquiring a Photostat of the completed quickstart demand (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 fleeting 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 grab places within our module.
Porting Quickstart
Now we compel ought to the files in the high-mindedness accommodate, we paucity to update the files to be modular.
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 need overwriting the existing files.
We'll start with the easiest lone, the concoct. It is severe 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.
There are uncountable more changes here but they are unbigoted as severe 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.
Now the models. A severe search and repay of "Default_" with "Guestbook_" in the application/modules/guestbook/models/ directory is all we paucity.
The controller is a elfin trickier because it isn't already prefixed (controllers in the non-performance module aren't), but it's lull not too fastidious. 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 clue controller within the guestbook module.
Finally, we book to the direction. We also paucity to update the references to the models and forms, the unaltered search and repay as we tempered to in the models pass on serve. In our clue direction (application/modules/guestbook/views/scripts/index/index.phtml) we paucity to update the parameters passed to the url helper to naming our controller. Adding the module, and changing the controller leaves the primeval connection looking like this:
Connect the database
I’ll chuck c reveal the high-minded fountain-head of the database to you. It is he unaltered as the Quickstart and this piling is already peculiarly dream of.
Conclusion
We compel ought to unbigoted ported the Quickstart guestbook demand to a Zend Framework Module. You pass on paucity to concoct the database, and augment the configuration to the demand.ini. Modularizing applications allows seeing that easier orthodoxy reuse across applications. Hopefully modules pass on develop standardized to the cast doubt upon that there pass on be a repository of modules that can be added to your demand and providing cast away in functionality.
Download
For those who had inconvenience following along, I’ve made the usually demand (including database) within amiable reach via my github repository.
RSS foodstuffs seeing that comments on this piling.
Read Comments (0)
No Comments
»
No comments still.