When to Create a New Controller in ZF2?

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.

Controller Registration

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.

Advertisements

I am a software developer currently living in Tomsk, Russia. I received a PhD degree in Computer Science from Tomsk Polytechnic University in 2010. I have been professionally developing C/C++ and PHP software since 2005. I like contributing to open-source and writing programming articles for popular web resources, like CodeProject. Besides writing, I love skiing and watching Formula-1.

Tagged with: ,
Posted in PHP, Zend Framework 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow ValuableThing on WordPress.com
%d bloggers like this: