我无法通过 ZendFramework2 url view helper 解析路由
Zend\View\Helper\Url
当我尝试通过以下方式解决我的路线时:
{$route['action'] = 'install'}
{$route['id'] = $this->escapeHtml($project->__get('id'))}
{$href = $this->url('projects', $route)}
(我使用 smarty3 作为模板)
我的路由配置如下所示:
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Projects\Controller\Projects',
'action' => 'index',
),
),
),
'projects' => array(
'type' => 'literal',
'options' => array(
'route' => '/projects',
'defaults' => array(
'controller' => 'Projects\Controller\Projects',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'install' => array(
'type' => 'segment',
'options' => array(
'route' => '/install/[:projectId]',
'constraints' => array(
'projectId' => '[0-9]+'
),
'defaults' => array(
'controller' => 'Projects\Controller\Projects',
'action' => 'install',
'projectId' => '0'
),
),
),
'defaults' => 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(
),
),
),
),
),
...
助手只是可以解决我到 /projects 的路线,他没有看到子路线。
在一些 var_dumping 之后,我看到我的 TreeRouteStack 有一个 Part 对象,其中包含我想要的路线。
Zend\Mvc\Router\Http\Part#133 (9) {
protected $route => class Zend\Mvc\Router\Http\Literal#132 (2) {
protected $route => string(9) "/projects"
protected $defaults => array(2) {
'controller' => string(28) "Projects\Controller\Projects"
'action' => string(5) "index"
}
}
protected $mayTerminate => bool(true)
protected $childRoutes => NULL
protected $baseUrl => NULL
protected $requestUri => NULL
protected $routes => class Zend\Mvc\Router\PriorityList#134 (4) {
protected $routes => array(2) {
'install' => array(3) { ... }
'defaults' => array(3) { ... }
}
protected $serial => int(2)
protected $count => int(2)
protected $sorted => bool(false)
}..
不幸的是,我将我的路线声明为子路线,但该部分没有任何子路线。我正在寻找的路线在 $routes 变量中。我在路由中做错了什么,它现在分裂错了?因此 url-helper 无法访问。
编辑:
如果我在没有帮助者的情况下解决我的路线,我可以访问 /projects/install/[id] 路线。