Get full URL Laravel in blade view file template

Posted on

Getting the full URL (Uniform Resource Locator) path of a web-based application is the exactly this article written for. The article will show how to get the full URL in a blade view file template in a web-based application powered by a Laravel framework. It is very useful when the application need to be directed or forwarded to another page or location. Normally the page or the location which is going to be directed or forwarded exist in the form of a link.

So, how can a blade view file template located in ‘resources/views’ can retrieved the full URL of the page ?. It can be done by using the keyword provided as one of the reserved keyword and it is must be written inside the special tag used in a blade view file template.

The reserved keyword used is ‘url’ and it is enclosed in a tag which is defined specifically in a blade view file template which is the ‘{{ }}’ tag. To be more precise on giving the example, below is a snippet code extracted from a file named ‘home.blade.php’ which is a file presented directly after successfully logged in to a web-based application powered by Laravel framework with the authentication feature activated or installed.

Below is the snippet code showing a link to logout from the web-based application :

<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>

So, the link which is going to be generated in the web-based application above is : ‘http://localhost/myapps/logout’. The value generated by only declaring the following url is : ‘http://localhost/myapps’ if the declaration is only given in the following snippet code :

{{ url('/') }}

So, in other words, the above tag declaration or definition will be translated as ‘http://localhost/myapps’. The reason that the link is defined with an additional string of ‘logout’ is because it is a link functioned for logging out from the web-based application. And furthermore it is actually has already been added as part of the route defined as the following command ‘php artisan make:auth’ is executed which can be seen completely the main purpose of executing that command in the article titled ‘Laravel login page example tutorial for Laravel 5.3’ in this link.

Then how the list of available or existing route can be known or can be found out ?. Simply by executing the following command ‘php artisan route:list’ below the variety of available or existing route which is active can be seen as shown below :

+--------+----------+------------+----------+-----------+-------------+
| Domain | Method   | URI        | Name     | Action    | Middleware  |
+--------+----------+------------+----------+-----------+-------------+
...
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
...

So, the URL with the additional ‘/logout’ really exist and to be able to access it in the full URL, just enclose it inside the “{{ url(‘/logout’) }}”.

One thought on “Get full URL Laravel in blade view file template

Leave a Reply