Zend Framework 2 onBootstrap重定向与路由中的动态变量

时间:2014-10-02 17:08:28

标签: php zend-framework2

我在Module.php文件中的onBootstrap函数中有一个重定向,如下所示:

$url = $e->getRouter()->assemble(array(), array('name' => 'administrator/error/acl')); // redirect to this URL
$response=$e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();

// stop further execution
$stopCallBack = function($event) use ($response){
    $event->stopPropagation();
    return $response;
};
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, $stopCallBack,-10000);

return $response;

虽然路由配置如下所示(管理员已定义并可与其他路由一起使用):

'error'=> array(
    'type'    => 'Segment',
    'options' => array(
        'route'    => '/error',
        'defaults' => array(
            'controller' => 'MyAdmin\Controller\Error',
            'action' => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'error_name' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/[:error_name]',
                'constraints' => array(
                    'error_name' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true
        ),
    ),
),

当它尝试重定向时,我收到以下错误:

  

致命错误:未捕获的异常'Zend \ Mvc \ Router \ Exception \ RuntimeException',消息'路由名称'acl“未找到”......

我做错了什么?

1 个答案:

答案 0 :(得分:0)

name param应该是路由名称,在您的情况下error/error_name。我不太确定你的例子中是什么'administrator / error / acl',如果你想要的那个error_name参数应该出现在URL中,你想要:

$url = $e->getRouter()->assemble(array(), array(
    'name' => 'error/error_name',
    'error_name' => 'administrator/error/acl'
));