How to Get Base URL in Laravel 12

In this laravel tutorial, we’ll also see how to get base URL in laravel 12 with or without the http/https scheme, both in the controller and in the Blade file, using practical examples.

When building web applications with Laravel, it’s often necessary to generate links, redirect users, load assets, or work with APIs dynamically — all of which rely on having the correct base URL. The base URL represents the root address of your application, such as https://yourdomain.com, and acts as the foundation for building absolute URLs throughout your site.

Laravel provides several simple and flexible ways to access the base URL, whether you’re working in controllers, views. Understanding how to retrieve the base URL is essential for maintaining environment-specific configurations (local, staging, production), avoiding hardcoded links, and ensuring your application runs smoothly across different servers.

In this tutorial, we’ll explore different methods to get the base URL in Laravel and understand when to use each based on your use case.

let’s see the example code:

Laravel Get Base URL in Controller

In this step, I will show you three different ways to get base URL in Laravel controller. The first method uses a built-in helper function, second method uses the URL facade provided by Laravel and the third method uses Request object.

Method 1: Using url() Helper Function

You can directly use Laravel’s url() helper to get the base URL like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function getUrl(Request $request)
    {   
         $url1 = url("/");
       
        dd($url1);
    }
}//

How to Get Base URL in Laravel 12
Base URL in Controller

This method is simple and commonly used for generating links or redirecting users.

Method 2: Using URL Facade

Alternatively, you can use the URL facade, which offers more flexibility when working with URLs:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use URL;

class DemoController extends Controller
{
    public function getUrl(Request $request)
    {   
         $url2 = URL::to("/");
        
       
        dd($url2);
    }
}//

Output is similar to method 1.

"http://127.0.0.1:8000" 

Both methods will return the same result — the base URL of your application — and can be used interchangeably based on your preference.

Method 3: Using request() Helper and $request

you can use schemeAndHttpHost() method to get the base url :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function getUrl(Request $request)
    {   
         $url3 = $request->schemeAndHttpHost();
         $url4 = request()->schemeAndHttpHost();
       
        dd($url3,$url4);
    }
}//

both request helper and $request return the same output :

"http://127.0.0.1:8000" 
"http://127.0.0.1:8000" 

All three methods will return the base URL of your Laravel application. You can choose any of them based on your project requirements and coding style.

Laravel Get Base URL in Blade

In this step, I will show you two different methods to get the base URL directly inside a Laravel Blade file. These methods are commonly used for generating links, loading assets.

Method 1: Using url() Helper Function

You can use Laravel’s built-in url() helper function directly in your Blade view:

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Get Base URL </title>
</head>
<body>
    <h1>
        Path: <strong>{{ url('/') }}</strong>
</h1>
 
</body>
</html>

This will return the base URL of your application, for example:

How to Get Base URL in Laravel 12
Base URL in Blade File

Method 2: Using URL::to() Facade

If you prefer using facades, you can also use URL::to(‘/’) inside your Blade file:

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Get Base URL</title>
</head>
<body>    
 <h1>
        Path: <strong>{{ URL::to('/') }}</strong>
</h1>
</body>
</html>

This produces the similar output as shown in Method 1.

Path: http://127.0.0.1:8000

This method also returns the base URL and is useful when you want use the facade-based approach across your application.Both methods produce the same result, so you can choose the one that fits your coding style or project standards.

Laravel Get Base URL Without http/https

n this section, we will see how to get the base URL without the protocol (http/https) in Laravel. This is useful when you only need the host name (like yourdomain.com) without the scheme.

Laravel provides simple ways to achieve this using the request() helper and the $request object.

Method 1: Using request() helper in blade

You can get the base URL without http or https using request()->host(). This method is very useful and commonly used when only the domain name is required.

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Get Base URL Without http/https</title>
</head>
<body>
<h1>URL <strong>{{request()->host()}} </strong></h1>
</body>
</html>

This will return:

127.0.0.1

It excludes both http:// and https://.

Method 2: Using request() Helper in Controller

You can also use the request() helper in your controller method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class DemoController extends Controller
{
    public function getUrl(Request $request)
    {   
        //base url without http 
        $urlWithoutHttp = request()->host();
       
        dd($urlWithoutHttp1);
    }
}//

How to Get Base URL in Laravel 12
Base URL Without http/https

Method 2: Using $request in Controller

Alternatively, you can use the $request object if it’s injected into your controller method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class DemoController extends Controller
{
    public function getUrl(Request $request)
    {   
        //base url without http 
        $urlWithoutHttp = $request->host();
       
        dd($urlWithoutHttp);
    }
}//

This produces the same output as shown in Method 2.

"127.0.0.1"

All these methods will give you the domain name only, excluding the protocol.