重定向路径使用完整控制器路径(重定向为bjyauthorize)

时间:2013-07-04 03:11:37

标签: zend-framework2 router bjyauthorize

我在使用bjyauthorize重定向方面遇到了麻烦。我按照以下链接:How to redirect to the login page with BjyAuthorize并且能够重定向工作。

我面临的问题是重定向正在构建一个具有完整控制器路径的URL。

http://lh/user/login?redirect=/talent/my/Myapp%5CController%5CProfile/index

where my route and controller are below:
route: talent/my
controller: Myapp\Controller\Profile

构建重定向字符串的代码是(编辑:发布完整的onDispatchError函数):

public function onDispatchError(MvcEvent $e)
{
    // Do nothing if the result is a response object
    $result = $e->getResult();
    if ($result instanceof Response) {
        return;
    }

    $router = $e->getRouter();
    $match  = $e->getRouteMatch();

    // get url to the zfcuser/login route
    $options['name'] = 'zfcuser/login';
    $url = $router->assemble(array(), $options);

    // Work out where were we trying to get to
    $options['name'] = $match->getMatchedRouteName();

    $matchParams = $match->getParams();

    // [jh]
    // TODO: need to fix this in a smart way
    // have already posted the problem on stackoverflow: https://stackoverflow.com/questions/17461274/redirect-path-is-using-full-controller-path-redirect-for-bjyauthorize
    $newParams = array(
        'controller' => $matchParams["__CONTROLLER__"],
    );

    // [jh]
    // having an index in the url looks a bit unprofessional
    // so, here i'm removing it... code is a bit dirty
    if ( 'index' !=  $matchParams["action"] )
        $newParams['action'] = $matchParams["action"];


    $redirect = $router->assemble($newParams, $options);


    // set up response to redirect to login page
    $response = $e->getResponse();
    if (!$response) {
        $response = new HttpResponse();
        $e->setResponse($response);
    }
    $response->getHeaders()->addHeaderLine('Location', $url . '?redirect=' . $redirect);
    $response->setStatusCode(302);
}

在这里,我为以下内容添加了var_dump $匹配 - > getParams()方法:

array(4) {
  ["__NAMESPACE__"]=>
  string(14) "Myapp\Controller"
  ["controller"]=>
  string(22) "Myapp\Controller\Profile"
  ["action"]=>
  string(5) "index"
  ["__CONTROLLER__"]=>
  string(7) "profile"
}

$选项:

array(1) {
  ["name"]=>
  string(16) "route_my/default"
}

我可以更改参数,以便控制器'字段设置为' profile'解决这个问题。但是,我不认为这是最好的方法。

请指教!

谢谢,

贾斯汀

0 个答案:

没有答案