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?
You can access the url from the Request Object: public function handle($request, Closure $next) { $url = $request->url(); ... }
you can easily get current url in laravel 6, laravel 7, laravel 8 and laravel 9 application. $currentURL = Request::url(); dd($currentURL);
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.
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.
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 −
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With