URL stands for Uniform Resource Locator. A URL is nothing more than the address of a given unique resource on the Web.

Example of URL:
Example of Laravel URL

There are 2 Types of URL:
1. Relative URL

Ex. <a href="/images/profilepicture.png">Link Text</a>

2. Absolute URL

Ex. <a href="http://othersite.com"></a>

URL contains below things:

  • Protocol ( https:// )
  • Sub domain ( www )
  • Domain Name ( google.com )
  • Path / Page / Parameter ( abc/test/123 )

Laravel provides several helpers to assist you in generating/getting URLs for your application. The basic methods are current(), full(), url(), request(), etc. In the laravel blade and controller we’ve different ways to get the url.

At very first we need to check about generating URLs, then accessing the current URLs.

1. Generating URLs

The url helper is used to build the arbitrary URLs for the application and it’ll automatically add the protocol (http/https) and create the URL. Below is the example for this:

$getData = App\Models\User::find(encrypt(1)); 
echo url("/posts/{$getData->id}");

Above code will generate below url in the address bar:
example

2. Accessing URLs

In laravel we have a facade of URL by using that one we can generate urls. Below is facade which we need to use when we want to generate URL.

use Illuminate\Support\Facades\URL;

Ex. echo URL::to('');
Ex. echo URL::current();

We can access the current url in the different manner:

//We can access the base URL in the laravel as below.
Ex. echo url('');

// Get the current URL without the query string.
Ex. echo url()->current();

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

// Get the full URL for the previous request.
Ex. echo url()->previous();

There are helper functions are there which we can use in the blade files to generate URL.

full() with Helper and Query string parameters

Ex. {{ url('/login') }}
Ex. {{ asset('css/app.css') }}
Ex. {{ route('login') }}
Ex. <link href="{{ asset('css/app.css') }}" rel="stylesheet">
Ex. <a class="nav-link" href="{{ route('login') }}">Login</a>
Ex. <form method="POST" action="{{ url('/login') }}">

We can also get the URL by using Request facade.

use Illuminate\Support\Facades\Request;

Ex. Request::fullUrl() // Returns: http://laravel.dev/test?test=1
Ex. Request::url() // Returns: http://laravel.dev/test
Ex. Request::path() // Returns: test
Ex. Request::root() // Returns: http://laravel.dev

Another way to generate URLs through the routes and it’s also define with named route and without named route. The examples for the route is:

Ex.

- Without named route:

Route::get('/page', function (){
    // ...
});

- Named route:
Route::get('/page/{page}', function (Page $page) {
    // ...
})->name('page.show');

Get the URL for both:
echo url('/page'); 
echo route('page.show', ['page' => 3]);

Also, to get the current route:

Ex. 

use Illuminate\Support\Facades\Route;

Route::current();
Route::currentRouteName();
Route::currentRouteAction();

Support On Demand!

                                         
Laravel