Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the url in the middleware laravel

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 = Request::url();

and I used:

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

any ideas?

like image 494
TheDragoner Avatar asked Feb 28 '15 10:02

TheDragoner


People also ask

How do I find my middleware url?

You can access the url from the Request Object: public function handle($request, Closure $next) { $url = $request->url(); ... }

How do I find url in Laravel 8?

you can easily get current url in laravel 6, laravel 7, laravel 8 and laravel 9 application. $currentURL = Request::url(); dd($currentURL);

Where are Laravel middlewares located?

Laravel comes with a few inbuilt middlewares for maintenance, authentication and CSRF protection, which are all found in the app/Http/Middleware directory inside a laravel project folder. Laravel routes are located in the app/Http/routes.php file.

How does the route Helper Work in Laravel?

The route helper will automatically extract the model's route key: Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created.

How to register the route specific middleware in Laravel?

To register the route specific middleware, add the key and value to $routeMiddleware property. We have created AgeMiddleware in the previous example. We can now register it in route specific middleware property. The code for that registration is shown below. The following is the code for app/Http/Kernel.php −

How do I get the Laravel base URL?

There are multiple ways of accessing the Laravel base URL. You could use one of the following approaches: You can also use this in your Blade template as follows: This will return the value of the APP_URL defined in your .env file:


1 Answers

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

like image 137
Wader Avatar answered Oct 03 '22 01:10

Wader