dongyuan7981 2014-10-02 17:08
浏览 71

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

I have a redirection in the onBootstrap function in the Module.php file which looks like:

$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;

While the route configuration looks like this (administrator is defined and works fine with other routes):

'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
        ),
    ),
),

When it tries to redirect I get the following error:

Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\RuntimeException' with message 'Route with name "acl" not found' in......

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dse3168 2014-10-02 18:15
    关注

    The name param should be the route name, in your case error/error_name. I'm not quite sure what 'administrator/error/acl' is in your example, if it's the error_name param that should appear in the URL, you want:

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

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角