ZF3:具有子路由的控制器不起作用(ZF3: Controllers with child routes doesn't work)

编程入门 行业动态 更新时间:2024-10-25 02:20:43
ZF3:具有子路由的控制器不起作用(ZF3: Controllers with child routes doesn't work)

我是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', ], ], ];

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

在哪里可以看到调用的控制器是Admin \ Controller \ IndexController而不是Admin \ Controller \ ArticulosController

我究竟做错了什么?

更新1:

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

信息:

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

堆栈跟踪:

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))

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):

Zend的\查看\视图 - >渲染(对象(Zend的\查看\型号\视图模型))

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))

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))

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

9 {主}

I'm ZF2 developer and I'm migrating to ZF3 and I'm having troubles whith some controllers.

For example, I have this url: http://localhost/admin which calls to the correct controller (IndexController) and show the correct view. But If I want to associate this url: http://localhos/admin/articulo with ArticuloController doesn't work. When I call to this url: http://localhost/admin/articulo the controller called is AdminController and doesn't find the view.

OPTION 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):

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

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 ...

Where you can see that the controller called is Admin\Controller\IndexController and not Admin\Controller\ArticulosController

What am I doing wrong?

Update 1:

The option 3 configuration works fine!!! 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 ...

Message:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "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()

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))

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

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))

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))

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()

9 {main}

最满意答案

这是一个错字问题。 试试这个http://localhost/admin/articulos (注意结尾的“s” ),因为你的路由器是/admin/articulos ,它指向这个ArticulosController的indexAction() 。 这就是为什么这个URL http://localhost/admin/articulo (没有结尾“s” )无法发送。 视图结构应该是module/controller/action类型。

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(). That is why this url http://localhost/admin/articulo (without ending "s") was not able to dispatch. And the view structure should be of type module/controller/action.

更多推荐

本文发布于:2023-08-01 04:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   控制器   不起作用   work   routes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!