zendframework/ZendSkeletonApplication

can not set controller as parameter in module.config.php in zf3

Closed this issue · 4 comments

I follow this step to install the zf3 skelton:

  1. install composer
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"

2.install skelton
php composer.phar create-project -sdev --no-plugins --no-scripts zendframework/skeleton-application /opt/php/zf3
3. I changed the module.config.php and wanna to set a universal route rule for all the controller and all actions beyond the default application module

        'application' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/application[/:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
        ],

you can see the full configuration via https://github.com/jobsfan/zf3document/blob/master/src/module/Application/config/module.config.php

you can view https://www.wendangs.com, https://www.wendangs.com/application/,
but! https://www.wendangs.com/application/index and https://www.wendangs.com/application/index/index is 404 error.

How can I fixed this problem? Is there a standard configuration and folder structure I can follow?

Do you have a controller named 'index'? Note that no magic class resolution is performed: it must be defined in the controllers list.

yes! I have a controller named 'index', code is here https://github.com/jobsfan/zf3document/blob/master/src/module/Application/src/Controller/IndexController.php, actually, I didn't change the skeleton much, the structure is brand-new. I just wanna to configure a universal route rule that I just don't need to change the route rules when I create more controller or more action until a new module added, then I will write a similar route rules under the new one.

in zf2, this kind of route configuration can match all the controllers and actions beyond this module as long as you add the controller to the controllers

         'ztcommon' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/zhuanti[/:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*[a-zA-Z]',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Zhuanti\Controller',
                    'controller'    => 'Index',
                    'action'     => 'index',
                ),
            ),
        ),

@froschdesign
thanks for your reply! follow your advise, I add this part to my module.php which is in src folder comparing to the same file in application root folder. but it is not work!
the module.php you can see via https://github.com/jobsfan/zf3document/blob/master/src/module/Application/src/Module.php

The address to check the result is https://www.wendangs.com/application/index/index, still 404 I got.

Further more, I got a warning from nginx error log:
file_put_contents(data/cache/module-classmap-cache.application.module.cache.php): failed to open stream: No such file or directory

I add namespace to default like

       'application' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/application/[:controller[/:action]]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => [
                    '__NAMESPACE__' => 'Application\Controller', //这个好像是必须的
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
            ],
        ],

and add a invokables key to controller like:

'controllers' => [
    'factories' => [
        Controller\IndexController::class => InvokableFactory::class,
    ],
    'invokables' => [
        'Application\Controller\Index' => 'Application\Controller\IndexController',
    ],
],

the problem seem solved, of course add a onBootstrap function in module.php file in src folder.
Thank you! @froschdesign @Ocramius