覆盖Laravel RedirectResponse

时间:2016-08-16 22:15:19

标签: php laravel oauth urlencode laravel-socialite

我正在为一个新的Oauth提供者扩展Laravel Socialiste,并且在修改授权URL的简单问题上遇到了麻烦。

RedirectResponse生成的网址有%2C而不是+,例如https://mycustomerprovider.com/oauth/authorize?scope=blah%2Cblahagain,用于范围分隔符,因此我使用的特定提供商失败了,在此示例中为mycustomprovider

任何人都知道如何修改此授权网址以将%2C更改为+

return Socialite::driver('mycustomprovider')->redirect();

如果您var_dump(Socialite::driver('mycustomprovider')->redirect()),则包含以下内容:

RedirectResponse {#886 ▼
  #targetUrl: "http://"
  +headers: ResponseHeaderBag {#888 ▶}
  #content: """
    <!DOCTYPE html>\n
    <html>\n
        <head>\n
            <meta charset="UTF-8" />\n
            <meta http-equiv="refresh" content="1;url=https://mycustomerprovider.com/oauth/authorize?scope=blah%2Cblahagain" />\n
    \n
            <title>Redirecting to https://mycustomerprovider.com/oauth/authorize?scope=blah%2Cblahagain</title>\n
        </head>\n
        <body>\n
            Redirecting to <a href="https://mycustomerprovider.com/oauth/authorize?scope=blah%2Cblahagain</a>.\n
        </body>\n
    </html>
    """
  #version: "1.0"
  #statusCode: 302
  #statusText: "Found"
  #charset: null
}

1 个答案:

答案 0 :(得分:1)

您应该为此

使用简单的中间件
$('#SelectedTags').select2({
    tags: true,
    tokenSeparators: [','],
    placeholder: "Add your tags here"
});

App \ Http \ Middleware \ ReplaceMiddleware.php

php artisan make:middleware ReplaceMiddleware

控制器中。在 __ construct 方法

public function handle($request, Closure $next, $guard = null)
{
    $response = $next($request);
    $response->setContent(str_replace('%2C','+',$response->getContent()));
    return $response;
}

在App \ Http \ Kernel.php中。添加到$ routeMiddleware 数组

$this->middleware('replace_response');

你已经完成了。试试吧!