Posts Learn Components Snippets Categories Tags Tools new About
/

How to get base URL in Laravel

Learn how to get the base URL in Laravel application using the helper function

Created on Oct 18, 2021

35 views

In this snippet, you will learn how to get the base URL in the Laravel application. 

Using getSchemeAndHttpHost method

<?php

// get url using "getSchemeAndHttpHost" 
request()->getSchemeAndHttpHost();

Using url() Helper Method


You can also use the "url()" helper function method
<?php

// get the base url using the "url" helper
url('');
url('/');

Using config Helper method


You can make use of the "config()" helper method as well.
<?php

// get the url from the "app.url"
config('app.url');

Other: Accessing Current URL


There are other helper methods as well to get the "current" URL of a page and you can write your code as follows
// Get the current page URL without the query string
echo url()->current();

// Get the current page URL including the query string
echo url()->full();

// Get the full page URL for the previous page
echo url()->previous();

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)