Getting Started with Zend Framework
Modules
zend-mvc uses a module system to organise your main application-specificcode within each module. TheApplication module provided by the skeleton is usedto provide bootstrapping, error, and routing configuration to the wholeapplication. It is usually used to provide application level controllers forthe home page of an application, but we are not going to use the defaultone provided in this tutorial as we want our album list to be the home page,which will live in our own module.
We are going to put all our code into theAlbum module which will contain ourcontrollers, models, forms and views, along with configuration. We’ll also tweaktheApplication module as required.
Let’s start with the directories required.
Setting up the Album module
Start by creating a directory calledAlbum undermodule with the followingsubdirectories to hold the module’s files:
zf-tutorial/ /module /Album /config /src /Controller /Form /Model /view /album /albumTheAlbum module has separate directories for the different types of files wewill have. The PHP files that contain classes within theAlbum namespace livein thesrc/ directory. The view directory also has a sub-folder calledalbumfor our module's view scripts.
In order to load and configure a module, Zend Framework provides aModuleManager. This will look for aModule class in the specified modulenamespace (i.e.,Album); in the case of our new module, that means the classAlbum\Module, which will be found inmodule/Album/src/Module.php.
Let's create that file now, with the following contents:
namespace Album;use Zend\ModuleManager\Feature\ConfigProviderInterface;class Module implements ConfigProviderInterface{ public function getConfig() { return include __DIR__ . '/../config/module.config.php'; }}TheModuleManager will callgetConfig() automatically for us.
Autoloading
While Zend Framework provides autoloading capabilities via itszend-loader component, werecommend using Composer's autoloading capabilities. As such, we need to informComposer of our new namespace, and where its files live.
Opencomposer.json in your project root, and look for theautoload section;it should look like the following by default:
"autoload": { "psr-4": { "Application\\": "module/Application/src/" }},We'll now add our new module to the list, so it now reads:
"autoload": { "psr-4": { "Application\\": "module/Application/src/", "Album\\": "module/Album/src/" }},Once you've made that change, run the following to ensure Composer updates itsautoloading rules:
$ composer dump-autoloadConfiguration
Having registered the autoloader, let’s have a quick look at thegetConfig()method inAlbum\Module. This method loads theconfig/module.config.php fileunder the module's root directory.
Create a file calledmodule.config.php underzf-tutorial/module/Album/config/:
namespace Album;use Zend\ServiceManager\Factory\InvokableFactory;return [ 'controllers' => [ 'factories' => [ Controller\AlbumController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'template_path_stack' => [ 'album' => __DIR__ . '/../view', ], ],];The config information is passed to the relevant components by theServiceManager. We need two initial sections:controllers andview_manager. The controllers section provides a list of all the controllersprovided by the module. We will need one controller,AlbumController; we'llreference it by its fully qualified class name, and use the zend-servicemanagerInvokableFactory to create instances of it.
Within theview_manager section, we add our view directory to theTemplatePathStack configuration. This will allow it to find the view scriptsfor theAlbum module that are stored in ourview/ directory.
Informing the application about our new module
We now need to tell theModuleManager that this new module exists. This isdone in the application’sconfig/modules.config.php file which is providedby the skeleton application. Update this file so that the array it returnscontains theAlbum module as well, so the file now looks like this:
(Changes required are highlighted using comments; original comments from thefile are omitted for brevity.)
return [ 'Zend\Form', 'Zend\Db', 'Zend\Router', 'Zend\Validator', 'Application', 'Album', // <-- Add this line];As you can see, we have added ourAlbum module into the list of modules aftertheApplication module.
We have now set up the module ready for putting our custom code into it.
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!