How to Get Current URL in Laravel

In Laravel, you can get the current URL of the application by using the url() helper function or the Request facade.

Here's an example of using the url() helper function to get the current URL:


$currentUrl = url()->current();

This will return the current URL, including the query string.

Alternatively, you can use the Request facade to get the current URL:


use Illuminate\Support\Facades\Request;
$currentUrl = Request::fullUrl();

This will return the full URL, including the query string.

You can also use Request::url() to get the URL without the query string.


$currentUrl = Request::url();

Note that if your application is behind a load balancer or reverse proxy, you may need to configure Laravel to trust the proxy in order to get the correct URL. You can do this by setting the trusted_proxies configuration option in config/app.php.

Comments

Leave a Reply