How to check if the page is home page in Laravel

In most cases we don’t need to detect home page as there are separated views and controllers for each page already. Carrying out a task on a specific page means you should do it on the respective views and files. In my case, I need to print root URL to the navigation bar which exists in layout file and the URL should not be on front page.

Following are some ways to check if current page is home or front page:

Table of Contents

Use Request::is()

Request::is('/') method checks if current url is ‘/’ which means root path of domain. Our homepage normally resides at ‘/’.

//in view
{!! Request::is('/') ? '' : url('/') !!}

Request::is() is quite useful as it supports wildcard. You can use it to check if the pages belong to a section with the same url prefix with Request::is('jobs/*').

Get route name

//in route
Route::get('/', ['as'=>'homepage', 'uses'=>'HomeController@index']);

//in view file
@if(request()->route()->getName() == 'homepage')
   // 
@else
  //
@endif

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top