laravel 获取中间件laravel中的url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28780925/
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
get the url in the middleware laravel
提问by TheDragoner
I have my middleware and inside it I am trying to reach the current url of the page. so I did something like that:
我有我的中间件,在它里面我试图访问页面的当前 url。所以我做了类似的事情:
$url = Request::url();
$url = Request::url();
and I used:
我用过:
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Http\Request;
but I keep getting the following error:
但我不断收到以下错误:
Non-static method Illuminate\Http\Request::url() should not be called statically, assuming $this from incompatible context
Non-static method Illuminate\Http\Request::url() should not be called statically, assuming $this from incompatible context
any ideas?
有任何想法吗?
回答by igavriil
You can access the url from the Request Object:
您可以从请求对象访问 url:
public function handle($request, Closure $next)
{
$url = $request->url();
...
}
Request
object has also fullUrl()
and path()
methods. Choose the one that fit your needs
Request
对象也有fullUrl()
和path()
方法。选择一款适合您的需求
回答by Wader
In Laravel 5 the request is already passed into the handle()
function
在 Laravel 5 中,请求已经传递到handle()
函数中
class MyMiddleware {
public function handle($request, Closure $next)
{
$url = $request->url();
// Do stuff here
return $next($request);
}
}
Laravel 5 tries to move away from facades (e.g: Calls such as Request::url()
) in favour of using dependency injection, so you may notice some functions and such cannot be accessed the same as you did in 4.
Laravel 5 尝试远离 Facades(例如:调用诸如Request::url()
),转而使用依赖注入,因此您可能会注意到某些函数无法像在 4 中那样访问。
Heres quite a nice explanation of dependency injection in Laravel 5 https://mattstauffer.co/blog/laravel-5.0-method-injection
这是 Laravel 5 中依赖注入的一个很好的解释https://mattstauffer.co/blog/laravel-5.0-method-injection