Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework 2 add new controller

In which files do we need to make changes in order to add a new controller in Module and call it through URL.

Is there a way in which we add a new controller file and call it through url without changing any other configuration files.

As it will be very tedious to make changes in configuration files on every add or edit in controller files.

like image 831
Vijay Choudhary Avatar asked Sep 20 '12 07:09

Vijay Choudhary


1 Answers

I am adding a little clarification to what Daniel said on "make sure you've got a matching route that satisfies your purpose...". For my purpose I was trying to accommodate routes to module/Application/src/Application/Controller/IndexController.php and module/Application/src/Application/Controller/ProfileController.php, however I struggled to resolve anything to my ProfileController. It was also not clear if ZF2 could accommodate multiple Controllers in a single module. I thought surely it must and it does! Given the two above controllers this is how I crafted my 'router' array inside module/Application/config/module.config.php.

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),

        'profile' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/profile',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Profile',
                    'action'        => 'index',
                ),
            ),
        ),
    ),
),

Also this is what my 'controllers' invokables looks like in modules.config.php following success.

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Profile' => 'Application\Controller\ProfileController'
    ),
),
like image 88
user1815615 Avatar answered Sep 18 '22 00:09

user1815615