【发布时间】:2015-11-29 18:20:08
【问题描述】:
我正在寻求有关 ZF2 路由器配置的帮助。
我想在开发环境中将多个控制器添加到单个模块,但最终必须弄清楚如何指定特定路由。
为此,我目前正在尝试使用 MVC 快速入门文档中指定的通用 module.config。但是,这似乎不像 ZF2 文档所建议的那样执行。
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html
文档说明:
“ZendSkeletonApplication 带有一个“默认路由”,可能 让你采取这个行动。那条路线基本上预计 “/{module}/{controller}/{action}”,允许您指定: “/zend-用户/你好/世界”。我们将主要在这里创建一条路线 出于说明目的,创建显式路由是 推荐的做法。”
但是,控制器渲染 /landlord, /landlord/home/index,但不是 /landlord/sandbox/index。 Home 和沙盒是控制器。沙盒已按照命名约定“SandboxController”进行命名。我的看法是,文档中代码的 child_routes 部分可能需要一些我错过的修改。
在沙盒实例中,我在 404 错误页面上收到此错误。
Landlord\Controller\Sandbox(解析为无效的控制器类或 别名:地主\控制器\沙盒)
我尝试将 'Landlord\Controller\Sandbox' => 'Landlord\Controller\SandboxController' 添加到可调用对象中,但这会产生错误。
下面列出了我的控制器:
return array(
'controllers' => array(
'invokables' => array(
'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
),
),
'router' => array(
'routes' => array(
'landlord' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/landlord',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Landlord\Controller',
'controller' => 'home',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'landlord' => __DIR__ . '/../view',
),
),
);
如果有直接配置 url 的简单方法,这也会很有用,或者非常好的教程也将不胜感激。
【问题讨论】:
标签: module zend-framework2 router