我是ZF2開發人員,我正在遷移到ZF3,我遇到了一些控制器的麻煩。ZF3:與子路線的控制器不起作用
例如,我有這個url:http://localhost/admin它調用正確的控制器(IndexController)並顯示正確的視圖。但是,如果我想關聯此網址:http://localhos/admin/articulo與ArticuloController不起作用。當我打電話給這個url:http://localhost/admin/articulo時,被調用的控制器是AdminController,並且沒有找到該視圖。
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樣式):
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(遵循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,我得到的是視圖...
在這裏您可以看到,所謂的控制器是Admin \控制器\的IndexController和不是Admin \ Controller \ ArticulosController
我在做什麼錯?
更新1:
選項3配置工作正常!我已經刪除/緩存目錄中的所有內容,現在該控制器被發現,但......我有現在的錯誤渲染模板...
消息:
的Zend \查看\ Renderer \ PhpRenderer :: render:無法呈現模板 「admin/articulos/index」;解析器無法解析到一個文件
堆棧跟蹤:
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 \ 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 /無功/網絡/ HTML/31juegos /供應商/ zendframework/Zend的-eventmanager進行/src/EventManager.php(322): Zend的\的mvc \視圖\ HTTP \ DefaultRenderingStrategy->渲染(對象(的Zend \的mvc \ MvcEvent))
5 /無功/網絡/ HTML/31juegos /供應商/ zendframework /zend-eventmanager/src/EventManager.php(171): 的Zend \ eventmanager進行\ EventManager-> triggerListeners(對象(的Zend \的mvc \ MvcEvent))
6 /無功/網絡/ HTML/31juegos /供應商/ zendframewo RK/Zend的-MVC/SRC/Application.php(367): 的Zend \ eventmanager進行\ EventManager-> triggerEvent(對象(的Zend \的mvc \ MvcEvent))
7 /無功/網絡/ HTML/31juegos /供應商/ zendframework/Zend的-MVC/SRC/Application.php(348): Zend的\的mvc \應用 - > completeRequest(對象(的Zend \的mvc \ MvcEvent))
8 /無功/網絡/ HTML/31juegos /公共/的index.php(40):的Zend \的mvc \應用 - >運行()
9 {主}
我認爲這是封閉的,因爲這個問題是由不能再現的問題或簡單的印刷錯誤造成的。 – halfer