When your site grows in size, you should create new controller classes instead of putting all actions to
IndexController. The Index controller is used for defining the actions which work for your entire site. It is recommended to create new controller class for each model (or for most important ones) of your business logic domain.
For example, you can create
UserController to manage users of your site. This controller would have the default “index” action for displaying the page with all users, “add” action for adding a new user, “edit” action for editing user’s profile and “delete” action for deleting the user.
By analogy, you would create
PurchaseController and its actions to manage the purchases of your products and implementing the shopping cart,
DownloadController and its actions to manage file downloads for the site, etc.
All controller classes belonging to a module should be registered in the module.config.php
configuration file as follows:
<?php return array( // ... 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController' // Put other controllers registration here ), ), // ... );
In line 5, we have the the controllers key, which contains the invokables subkey. You should register your controllers here. To register a controller class, you add the line in form of key=>value pair. The key should be the unique ID of the controller, like Application\Controller\Index, and value should be the fully qualified class name of the controller, like Application\Controller\IndexController.
By registering your controller under the invokables subkey, you tell Zend Framework that it can invoke the controller by instantiating it with the
new operator. This is the most simple way of instantiating the controller. As an alternative, you can register a factory to create the controller instance, in that case you would register your controller under the factories subkey.