简体   繁体   中英

Zend Framework 2 onBootstrap redirection with dynamic variable in route

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM