Home  >  Article  >  Backend Development  >  How to get the current URL in Laravel's @if statement?

How to get the current URL in Laravel's @if statement?

PHPz
PHPzforward
2023-08-23 14:13:39925browse

How to get the current URL in Laravels @if statement?

To get the current URL you can use the method explained in the example below −

Example 1

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; use IlluminateHttpResponse; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

test.blade.php is −

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (Request::path() == 'users') <h1>The path is users</h1> @endif </div> </body> </html> 

In the test.blade.php file, use Request::path() to check if it points to the user and then only display the h1 tag. Request::path() Returns the URL currently in use.

Example 2

In this example, let's use the url()->current() method as shown in the example below. url()->current() Returns the full path of the current URL.

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (url()->current() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 

When executing the above example, it will print the following on the browser −

The path is users

Example 3

In this example, we will use Request::url(). Its output is the same as url()->current(), which returns the full URL as shown in the example below −

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::url() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 

When executing the above example, it will print the following on the browser −

The path is users

Example 4

Use Request::is()

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller{ public function index(Request $request) { return view('test'); } } 

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::is('users')) <h1>The path is users</h1> @endif </div> </body> </html> 

In the above example, Request::is() is used. It returns true/false indicating whether the given string exists in the URL.

When executing the above example, it will print the following on the browser −

The path is users


The above is the detailed content of How to get the current URL in Laravel's @if statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete