How to get base url in Laravel?

In this tutorial, explain how get base URL in Laravel. We will learn how to get site URL in Laravel. You will understand the concept of getting the base URL in a Laravel controller. This article provides an easy example of  getting the base URL in a Laravel view.

To get the base URL in Laravel, you can use various methods provided by the Laravel framework. Here are some common methods to achieve this. Laravel provides several methods to retrieve the base URL, making it easy to work with URLs dynamically in your application.

1. Get base url in laravel controller file

Base URL refer to root address of your application, consisting of the protocol (http or https), domain name, and, if applicable, a subdirectory.

Example:
/* Get base URL using URL Facades */
$url = URL::to("/");

/*Get Base URL using url() helper */
$url2 = url('/');

dd($url, $url2);

Explanation: 

  • url(‘/’): Generates the base URL by passing ‘/’ to the url() helper.
  • This method is work well in controllers or any PHP class.
2 . Get base URL in laravel using the request Helper

The ‘request’ helper can be used the get the full URL of the current request, including the query string. However, if you need the base URL, you can get it like this:

$base_url = request()->root();
3 . Get base URL in laravel blade file

We have explained of  how to get base URL in Laravel blade file. If you need to get URL inside a blade file, so you can use the example below.

Example
{{URL::to("/")}}

{{ url('/') }}
Explanation: 
  • Blade templates support PHP code and Laravel helpers, making it easy to integrate base URLs into your views.
  • Using {{ url(‘/’) }} directory if often the most concise method.
4. Laravel get base URL using configuration

You can also get base URL in Laravel from the configuration, which is set in the ‘config/app.php’ file. This can be useful if you have it defined there.

Example
$base_url = config('app.url');

Explanation: 

  • APP_URL is standard environment variable in Laravel configuration.
  • config(‘app.url’) retrieves the value set in APP_URL.
  • This approach different parts of your application.
5. Access the current URL in Laravel

The ‘url()’ helper function comes with a lot of methods that you could use. Here are some of the most comman ones:

Get the current URL:
echo url()->current();

Can aslo be used in blade template directly as follows:

{{ url()->current }}

Get the current URL, including the GET parameters:

echo url()->full();
6. Access assets URLs

In most time, when referring to static assets stored in your public folder, like images, PDFs, etc., the best approach is to use the ‘asset()’ helper. You can consider following example.

<img src="{{ asset('test.png') }}"

This will return your domain name and image path here. This url return base url.

I hope this tutorial help you.

7. Using the app() helper function with UrlGenerator class in Laravel
$base_url = app('url')->to('/');
8. Using the Request instance in Laravel
use Illuminate\Support\Facades\Request;

$base_url = Request::root();

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *