Home / Snippets / How to get base URL in Laravel
How to get base URL in Laravel cover

How to get base URL in Laravel

1.5K

3 years ago

0 comments

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();
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this