Laravel 如何删除 url 查询参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44494064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel how to remove url query params?
提问by Donne
I request api to check user , and the backurl will add a query param tokenlike this :
我请求 api 检查 user ,并且 backurl 将添加一个查询参数 令牌,如下所示:
www.test.com?store_id=2&token = 123
I want to show this
我想展示这个
www.test.com?store_id=2
I handle it in middleware , I wish there is a mothod to remove tokenbefore return $next($request)
but I didn't find the method. And I can't just use some method to delte this params and redirect , it will make a redirect loop.
if there is no better method, maybe I will create a new method in LoginController to remove token and redirect to where the page I from.
我处理它在中间件,我希望有是消除评判令牌之前return $next($request)
,但我没有找到方法。而且我不能只使用某种方法来删除此参数和重定向,它会进行重定向循环。如果没有更好的方法,也许我会在 LoginController 中创建一个新方法来删除令牌并重定向到我来自的页面的位置。
回答by apokryfos
You can have some sort of global middleware:
您可以拥有某种全局中间件:
class RedirectIfTokenInRequest {
public function handle($request,$next) {
if ($request->token) {
return redirect()->to(url()->current().'?'.http_build_query($request->except("token")));
}
return $next($request);
}
}
This will just redirect if there's a token parameter there. If you need to store it somehow you can use session(["token" => $request->token]);
to store it before your redirect.
如果那里有令牌参数,这只会重定向。如果您需要以某种方式存储它,您可以session(["token" => $request->token]);
在重定向之前使用它来存储它。
回答by Marek Gralikowski
Middleware is the best option. You can attach middleware class, to routes, in web or to single method. My middleware proposal:
中间件是最好的选择。您可以将中间件类附加到路由、Web 或单个方法。我的中间件建议:
namespace App\Http\Middleware;
use Closure;
class ClearFromAttributes
{
/**
* Remove some attributes which makes some confusion.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->get('unwanted_param')) {
return $this->removeFromQueryAndRedirect($request, 'unwanted_param');
}
if ($request->has('second_unwanted')) {
return $this->removeFromQueryAndRedirect($request, 'second_unwanted');
}
return $next($request);
}
/**
* Remove and make redirection.
*
* @param \Illuminate\Http\Request $request
* @param string $parameter
* @return mixed
*/
public function removeFromQueryAndRedirect($request, string $parameter)
{
$request->query->remove($parameter);
return redirect()->to($request->fullUrlWithQuery([]));
}
}
Of course, I have more complicated conditions in the handle
method, in reality.
当然,我在handle
方法上有更复杂的条件,在现实中。
Usage in controller constructor without touching Kernel file:
在不接触内核文件的情况下在控制器构造函数中使用:
$this->middleware(ClearFromAttributes::class)->only('index');
This is a nice option, for single usage.
这是一个不错的选择,适合单次使用。