get the url in the middleware laravel get the url in the middleware laravel laravel laravel

get the url in the middleware laravel


You can access the url from the Request Object:

 public function handle($request, Closure $next) {      $url = $request->url();      ... }

Request object has also fullUrl() and path() methods. Choose the one that fit your needs


In Laravel 5 the request is already passed into the handle() function

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.

Heres quite a nice explanation of dependency injection in Laravel 5 https://mattstauffer.co/blog/laravel-5.0-method-injection