简体   繁体   English

ZF3:具有子路由的控制器不起作用

[英]ZF3: Controllers with child routes doesn't work

I'm ZF2 developer and I'm migrating to ZF3 and I'm having troubles whith some controllers. 我是ZF2开发人员,正在迁移到ZF3,遇到一些控制器遇到的麻烦。

For example, I have this url: http://localhost/admin which calls to the correct controller (IndexController) and show the correct view. 例如,我有以下URL: http:// localhost / admin ,它调用正确的控制器(IndexController)并显示正确的视图。 But If I want to associate this url: http://localhos/admin/articulo with ArticuloController doesn't work. 但是,如果我想将此URL与ArticuloController关联: http:// localhos / admin / articulo不起作用。 When I call to this url: http://localhost/admin/articulo the controller called is AdminController and doesn't find the view. 当我调用此URL: http:// localhost / admin / articulo时 ,调用的控制器是AdminController,但找不到该视图。

OPTION 1 => module.config.php: 选项1 => module.config.php:

namespace Admin;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'admin' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/admin[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'admin/articulos' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/admin/articulos[/:action]',
                    'defaults' => [
                        'controller' => Controller\ArticulosController::class,
                        'action'     => 'index',
                    ],
                ],
            ],            
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\ArticulosController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout-admin.phtml',
            'admin/index/index'       => __DIR__ . '/../view/admin/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        /*
         * Con este array de parámetros permitimos enviar datos y no mostrar vista
         */
        'strategies' => [
            'ViewJsonStrategy',
        ],           
    ],
];

OPTION 2 => module.config.php (ZF2 style): 选项2 => module.config.php(ZF2样式):

namespace Admin;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'admin' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/admin[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'admin/articulos' => [
                'type'    => Literal::class,
                'options' => [
                    'route'    => '/admin/articulos[/:action]',
                    'defaults' => [
                        'controller' => 'Articulos',
                        'action'     => 'index',
                    ],                 
                ],
                'may_terminate' =>  true,
                'child_routes'  =>  [
                    'default'   =>[
                        'type'  =>  Segment::class,
                        'options'   =>  [
                            'route' =>  '/[:controller[/:action][/:id1]]',
                            'constraints'   =>  [
                                'controller'    =>  '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'        =>  '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id1'           =>  '[0-9_-]*'
                            ],
                            'defaults'  =>  [],
                        ],
                    ],
                ],
            ],            
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\ArticulosController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout-admin.phtml',
            'admin/index/index'       => __DIR__ . '/../view/admin/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        /*
         * Con este array de parámetros permitimos enviar datos y no mostrar vista
         */
        'strategies' => [
            'ViewJsonStrategy',
        ],           
    ],
];

OPTION 3 => module.config.php (following zf3 tutorial): https://docs.zendframework.com/zend-mvc/routing/#http-routing-examples 选项3 => module.config.php(遵循zf3教程): https ://docs.zendframework.com/zend-mvc/routing/#http-routing-examples

namespace Admin;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'admin' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/admin[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
                'may_terminate' =>  true,
                'child_routes'  =>  [
                    'articulos' =>  [
                        'type'  =>  Segment::class,
                        'options'   =>  [
                            'route' =>  '/articulos[/:action]',
                            'defaults'  =>  [
                                'controller'    => Controller\ArticulosController::class,
                                'action'        =>  'index'
                            ],
                        ],
                    ],
                ],
            ],           
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
            Controller\ArticulosController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout-admin.phtml',
            'admin/index/index'       => __DIR__ . '/../view/admin/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        /*
         * Con este array de parámetros permitimos enviar datos y no mostrar vista
         */
        'strategies' => [
            'ViewJsonStrategy',
        ],           
    ],
];

For all of the configurations when I call the url: http://localhost/admin/articulos the view that I get is ... 对于所有配置,当我调用url时: http:// localhost / admin / articulos我得到的视图是...

在此处输入图片说明

Where you can see that the controller called is Admin\\Controller\\IndexController and not Admin\\Controller\\ArticulosController 在这里您可以看到调用的控制器是Admin \\ Controller \\ IndexController,而不是Admin \\ Controller \\ ArticulosController

What am I doing wrong? 我究竟做错了什么?

Update 1: 更新1:

The option 3 configuration works fine!!! 选项3的配置工作正常!!! I have delete all the content from /cache directory and now the controller is found but ... I have got now an error rendering the template ... 我已经从/ cache目录中删除了所有内容,现在找到了控制器,但是...现在呈现模板时出现错误...

Message: 信息:

Zend\\View\\Renderer\\PhpRenderer::render: Unable to render template "admin/articulos/index"; Zend \\ View \\ Renderer \\ PhpRenderer :: render:无法呈现模板“ admin / articulos / index”; resolver could not resolve to a file 解析器无法解析为文件

Stack Trace: 堆栈跟踪:

0 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(207): Zend\\View\\Renderer\\PhpRenderer->render() 0 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(207):Zend \\ View \\ Renderer \\ PhpRenderer-> render()

1 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(236): Zend\\View\\View->render(Object(Zend\\View\\Model\\ViewModel)) 1 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(236):Zend \\ View \\ View-> render(Object(Zend \\ View \\ Model \\ ViewModel))

2 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(200): Zend\\View\\View->renderChildren(Object(Zend\\View\\Model\\ViewModel)) 2 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(200):Zend \\ View \\ View-> renderChildren(Object(Zend \\ View \\ Model \\ ViewModel))

3 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(105): 3 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(105):

Zend\\View\\View->render(Object(Zend\\View\\Model\\ViewModel)) Zend \\ View \\ View-> render(Object(Zend \\ View \\ Model \\ ViewModel))

4 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\\Mvc\\View\\Http\\DefaultRenderingStrategy->render(Object(Zend\\Mvc\\MvcEvent)) 4 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(322):Zend \\ Mvc \\ View \\ Http \\ DefaultRenderingStrategy-> render(Object(Zend \\ Mvc \\ MvcEvent))

5 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\\EventManager\\EventManager->triggerListeners(Object(Zend\\Mvc\\MvcEvent)) 5 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(171):Zend \\ EventManager \\ EventManager-> triggerListeners(Object(Zend \\ Mvc \\ MvcEvent))

6 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(367): Zend\\EventManager\\EventManager->triggerEvent(Object(Zend\\Mvc\\MvcEvent)) 6 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(367):Zend \\ EventManager \\ EventManager-> triggerEvent(Object(Zend \\ Mvc \\ MvcEvent))

7 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(348): Zend\\Mvc\\Application->completeRequest(Object(Zend\\Mvc\\MvcEvent)) 7 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(348):Zend \\ Mvc \\ Application-> completeRequest(Object(Zend \\ Mvc \\ MvcEvent))

8 /var/www/html/31juegos/public/index.php(40): Zend\\Mvc\\Application->run() 8 /var/www/html/31juegos/public/index.php(40):Zend \\ Mvc \\ Application-> run()

9 {main} 9 {main}

在此处输入图片说明

It is a typo issue. 这是一个错字问题。 Try with this http://localhost/admin/articulos (note the ending "s" ) because your router is /admin/articulos which points to this ArticulosController 's indexAction() . 尝试使用此http://localhost/admin/articulos (注意结尾的“ s” ),因为您的路由器是/admin/articulos ,它指向此ArticulosControllerindexAction() That is why this url http://localhost/admin/articulo (without ending "s" ) was not able to dispatch. 这就是为什么该URL http://localhost/admin/articulo (不带“ s” )无法发送的原因。 And the view structure should be of type module/controller/action . 并且视图结构应为module/controller/action类型。

(Posted on behalf of the OP) . (代表OP张贴)

Finally, I have fixed my last problem. 最后,我解决了我的最后一个问题。 The problem was due to my index.phtml was in an wrong directory /view/admin/articulos/**index/**index.phtml . 问题是由于我的index.phtml位于错误的目录/view/admin/articulos/**index/**index.phtml The correct directory is /view/admin/articulos/index.phtml . 正确的目录是/view/admin/articulos/index.phtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM