
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Current URL Inside If Statement in Laravel
To get the current URL you can use the methods explained in the example below ?
Example 1
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller { public function index(Request $request) { return view('test'); } }
The 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>
Inside the test.blade.php the Request::path() is used to check if it points to users and then only the h1 tag is displayed. The Request::path() returns the current URL being used.
Example 2
In this example let us make use of url()->current() method as shown in the example below. The url()->current() gives the current URL full path.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; 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>
On executing the above example, it prints the following on the browser ?
The path is users
Example 3
In this example will make use of Request::url() . The output of it is same as url()->current() , it return the full URL as shown in the example below ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; 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>
On executing the above example, it prints the following on the browser ?
The path is users
Example 4
Using the Request::is()
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; 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 example above Request::is() is used. It returns true/false if the given string is present in the URL.
On executing the above example, it prints the following on the browser ?
The path is users