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

时间:2017-05-03 15:03:20

标签: php model-view-controller configuration zend-framework3

我是ZF2开发人员,我正在迁移到ZF3,而且我遇到了一些控制器问题。

例如,我有这个url:http://localhost/admin,它调用正确的控制器(IndexController)并显示正确的视图。但是,如果我想将此网址关联:http://localhos/admin/articulo与ArticuloController不起作用。当我调用这个url:http://localhost/admin/articulo时,调用的控制器是AdminController,但找不到视图。

选项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',
        ],           
    ],
];

选项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',
        ],           
    ],
];

选项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',
        ],           
    ],
];

对于我调用网址的所有配置:http://localhost/admin/articulos我得到的视图是...

enter image description here

您可以看到所调用的控制器是Admin \ Controller \ IndexController而不是Admin \ Controller \ ArticulosController

我做错了什么?

更新1:

选项3配置工作正常!!!我已经删除了/ cache目录中的所有内容,现在找到了控制器但是...我现在收到了渲染模板的错误......

消息:

  

Zend \ View \ Renderer \ PhpRenderer :: render:无法呈现模板   “管理/ articulos /索引”;解析器无法解析为文件

堆栈追踪:

  

0 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(207):   的Zend \视图\渲染\ PhpRenderer->呈现()

     

1 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(236):   的Zend \视图\视图 - >呈现(对象(的Zend \视图\模型\视图模型))

     

2 /var/www/html/31juegos/vendor/zendframework/zend-view/src/View.php(200):   的Zend \视图\视图 - > renderChildren(对象(的Zend \视图\模型\视图模型))

     

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

     

的Zend \视图\视图 - >呈现(对象(的Zend \视图\模型\视图模型))

     

4 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(322):   的Zend \的mvc \视图\ HTTP \ DefaultRenderingStrategy->呈现(对象(的Zend \的mvc \ MvcEvent))

     

5 /var/www/html/31juegos/vendor/zendframework/zend-eventmanager/src/EventManager.php(171):   的Zend \ eventmanager进行\ EventManager-> triggerListeners(对象(的Zend \的mvc \ MvcEvent))

     

6 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(367):   的Zend \ eventmanager进行\ EventManager-> triggerEvent(对象(的Zend \的mvc \ MvcEvent))

     

7 /var/www/html/31juegos/vendor/zendframework/zend-mvc/src/Application.php(348):   的Zend \的mvc \应用 - > completeRequest(对象(的Zend \的mvc \ MvcEvent))

     

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

     

9 {main}

enter image description here

2 个答案:

答案 0 :(得分:1)

这是错字问题。尝试使用此http://localhost/admin/articulos(请注意结尾" s" ),因为您的路由器/admin/articulos指向此ArticulosController&#39}。 s indexAction()。这就是为什么这个网址http://localhost/admin/articulo(没有结束" s" )无法发送的原因。视图结构应为module/controller/action类型。

答案 1 :(得分:0)

(代表OP发布)

最后,我解决了我的最后一个问题。问题是由于我的index.phtml位于错误的目录/view/admin/articulos/**index/**index.phtml。正确的目录是/view/admin/articulos/index.phtml