一个模块中许多控制器的 ZF3 zend-mvc 通用路由不起作用
ZF3 zend-mvc generic route for many controllers in one module not working
我在 ZF3 中,使用 zend-mvc-skeleton 并尝试配置一个通用路由,该路由将匹配尽可能多的 URL,因为我希望能够创建新的控制器(当然包括操作方法) , 并立即提供。
文档中描述的常用方法是编写一个匹配控制器和动作的路由(与 ZF2 相同)。
这是我的 module.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'default' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:controller[/:action]]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
],
],
],
'controllers' => [/* ... */],
'view_manager' => [/* ... */],
],
对于 http://localhost/
和 http://localhost/application
调用 /module/Application/src/IndexController.php
文件中 IndexController
class 的 indexAction()
函数就像一个魅力.
但是,当我尝试在同一控制器(即 IndexController)中获取 fooAction()
函数时,它不起作用。它没有正确解析 http://localhost/application/foo
。我收到以下错误:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
foo (resolves to invalid controller class or alias: foo)
No Exception available
如果我尝试 http://localhost/bar/foo
获取 barController
中的 fooAction()
,同样的错误。
你知道这有什么问题吗?任何帮助将不胜感激。非常感谢。
路由 http://localhost/application/foo
不会在索引控制器中解析为 fooAction()
,因为 URL 中的 /foo
将匹配控制器而不是动作。使用该路线设置,您需要访问 http://localhost/application/index/foo
.
要使其正常工作,您还需要确保在配置中为您的控制器设置了别名,例如假设你有:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
]
],
然后为控制器设置别名,使其与路由参数相匹配:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
],
'aliases' => [
'index' => 'Application\Controller\Index'
]
],
您需要为每个未使用您想要的路由字符串注册的控制器添加与路由参数匹配的别名,例如控制器 Namespace\Controller\BarController
的别名应为 bar
,等等
我带着类似的问题来到这里。我在中创建了两个控制器
"Application" 模块,新模块 "Account" 中的两个同名。
Application/Controller/IndexController
Application/Controller/OverviewController
Account/Controller/IndexController
Account/Controller/OverviewController
这是我的 modules.config.php
module/Account/config/module.config.php
return [
'router' => [
'routes' => [
'Account-account' => [
'type' => Segment::class,
'options' => [
'route' => '/account[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Account\Controller',
'controller' => Account\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_us'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => AccountControllerFactory::class,
Controller\OverviewController::class => AccountControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class
]
],
还有我的
module/Application/config/module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'Application-application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Application\Controller',
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_US'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
Controller\OverviewController::class => IndexControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class,
]
],
使用此配置,如果对别名部分进行了注释,则会出现一条错误消息,指出存在无效的控制器或别名 (index/overview)。
如果有别名
路线:"application/overview/index" 进入帐户模块。
我在 ZF3 中,使用 zend-mvc-skeleton 并尝试配置一个通用路由,该路由将匹配尽可能多的 URL,因为我希望能够创建新的控制器(当然包括操作方法) , 并立即提供。
文档中描述的常用方法是编写一个匹配控制器和动作的路由(与 ZF2 相同)。
这是我的 module.config.php
namespace Application;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'default' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:controller[/:action]]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
],
],
],
'controllers' => [/* ... */],
'view_manager' => [/* ... */],
],
对于 http://localhost/
和 http://localhost/application
调用 /module/Application/src/IndexController.php
文件中 IndexController
class 的 indexAction()
函数就像一个魅力.
但是,当我尝试在同一控制器(即 IndexController)中获取 fooAction()
函数时,它不起作用。它没有正确解析 http://localhost/application/foo
。我收到以下错误:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
foo (resolves to invalid controller class or alias: foo)
No Exception available
如果我尝试 http://localhost/bar/foo
获取 barController
中的 fooAction()
,同样的错误。
你知道这有什么问题吗?任何帮助将不胜感激。非常感谢。
路由 http://localhost/application/foo
不会在索引控制器中解析为 fooAction()
,因为 URL 中的 /foo
将匹配控制器而不是动作。使用该路线设置,您需要访问 http://localhost/application/index/foo
.
要使其正常工作,您还需要确保在配置中为您的控制器设置了别名,例如假设你有:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
]
],
然后为控制器设置别名,使其与路由参数相匹配:
'controllers' => [
'invokables' => [
'Application\Controller\Index' => \Application\Controller\IndexController::class
],
'aliases' => [
'index' => 'Application\Controller\Index'
]
],
您需要为每个未使用您想要的路由字符串注册的控制器添加与路由参数匹配的别名,例如控制器 Namespace\Controller\BarController
的别名应为 bar
,等等
我带着类似的问题来到这里。我在中创建了两个控制器 "Application" 模块,新模块 "Account" 中的两个同名。
Application/Controller/IndexController
Application/Controller/OverviewController
Account/Controller/IndexController
Account/Controller/OverviewController
这是我的 modules.config.php
module/Account/config/module.config.php
return [ 'router' => [ 'routes' => [ 'Account-account' => [ 'type' => Segment::class, 'options' => [ 'route' => '/account[/][:controller[/][:action][/]]', 'defaults' => [ '__NAMESPACE__' => 'Account\Controller', 'controller' => Account\Controller\IndexController::class, 'action' => 'index', 'locale' => 'en_us' ], ], 'may_terminate' => true, 'child_routes' => [ 'wildcard' => [ 'type' => 'Wildcard' ], ], ], ], ], 'controllers' => [ 'factories' => [ Controller\IndexController::class => AccountControllerFactory::class, Controller\OverviewController::class => AccountControllerFactory::class, ], 'aliases' => [ 'index' => IndexController::class, 'overview' => OverviewController::class ] ],
还有我的 module/Application/config/module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'Application-application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/][:controller[/][:action][/]]',
'defaults' => [
'__NAMESPACE__' => 'Application\Controller',
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
'locale' => 'en_US'
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard'
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => IndexControllerFactory::class,
Controller\OverviewController::class => IndexControllerFactory::class,
],
'aliases' => [
'index' => IndexController::class,
'overview' => OverviewController::class,
]
],
使用此配置,如果对别名部分进行了注释,则会出现一条错误消息,指出存在无效的控制器或别名 (index/overview)。 如果有别名 路线:"application/overview/index" 进入帐户模块。