【发布时间】:2020-11-02 09:55:50
【问题描述】:
所以我正在尝试测试我的 laravel 的中间件,该中间件将返回重定向。
如果请求不满足中间件的要求,请求将使用redirect()->route('name')重定向
我尝试像这样从中间件获取返回值
$response = $middleware->handle($request, function ($req) { return $req; }, 'server:summary:view:edit');
\Log::info($response);
$response 是Illuminate\Http\RedirectResponse 的实例。
当我执行\Log::info($response); 时,我得到了这个结果。
HTTP/1.0 302 Found
Cache-Control: no-cache, private
Date: Mon, 13 Jul 2020 08:12:27 GMT
Location: https://myrandomurl.test/name
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://myrandomurl.test/name" />
<title>Redirecting to https://myrandomurl.test/name</title>
</head>
<body>
Redirecting to <a href="https://myrandomurl.test/name">https://myrandomurl.test/named</a>.
</body>
</html>
那么如何从 Location 标签中获取 url 呢?或如何从 laravel redirectResponse 获取重定向 url?
我尝试浏览https://laravel.com/api/5.8/Illuminate/Http/RedirectResponse.html#method_content 的文档,但没有显示重定向路由的方法。
我需要路由来断言它以获得成功的测试结果。
【问题讨论】:
-
Location 是一个标题,那么
$response->header('Location')怎么样? -
@apokryfos 没有用。
redirectResponse上的header()方法用于设置请求标头。没有得到。 -
-
@apokryfos 不能使用
$response->headers->get('Location'),因为redirectResponse没有headers属性。但是哦,你对assertRedirect()说得对。我应该用那个。那么我可能应该删除这个问题。