zend framework 2 add new controller

2019-02-10 08:09发布

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.

3条回答
我想做一个坏孩纸
2楼-- · 2019-02-10 08:30

From Zend Framework 2 reference

We inform the application about controllers we expect to have in the application. This is to prevent somebody requesting any service the ServiceManager knows about in an attempt to break the application. The dispatcher uses a special, scoped container that will only pull controllers that are specifically registered with it, either as invokable classes or via factories.

src : http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html#create-a-route

Adding the controllers is not related to routing, except tangentially due to developers wanting to be able to specify the controller in the URL. It's a security issue, due to the fact that we treat controllers as services.

This is an effort (a) to be explicit about what controllers are available, and (b) to be secure by default.

As sayed by Matthew Weier O'Phinney .

查看更多
一夜七次
3楼-- · 2019-02-10 08:33

To create a new controller, you have at least to

  • create the controller class
  • make sure you've got a matching route that satisfies your purpose (you can take existing ones as well as create a new one for special purposes)
  • create a controllers.invokables entry in your module's module.config.php.

There's no way around these three simple steps. I don't see why it should be a problem to adapt the module.config.php when you add a controller -- that's what config files are for. Including opening, saving und closing, this takes approx. 10-15 seconds. You aren't going to create hundreds of controllers, are you?

查看更多
We Are One
4楼-- · 2019-02-10 08:39

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'
    ),
),
查看更多
登录 后发表回答