0

我在使用 bjyauthorize 进行重定向时遇到问题。我点击了这个链接:How to redirect to the login page with BjyAuthorize and is able to get redirect work。

我面临的问题是重定向正在构建一个具有完整控制器路径的 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);
}

在这里,我为以下 $match->getParams() 添加了 var_dump:

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"
}

我可以更改参数,以便将“控制器”字段设置为“配置文件”以解决此问题。但是,我认为这不是最好的方法。

请指教!

谢谢,

贾斯汀

4

0 回答 0