How to get current Request URL or Route in Laravel 6

This tutorial will give you details on how to check current url or route in your laravel project, many time we needs to check current request url into our controller to do some action based on it.

Here will show you few different example of checking current requested urls or routes in laravel, this is very useful when it comes to play with menus or blade templates in laravel projects.

I assume that you already have laravel project up and running if not you checkout this tutorial to setup new laravel 6 application.

Follow – Develop Exclusive Laravel 6 CRUD Application with Input Validations

How to Get Current Request URL

Get current URL without the query string

echo \Request::url();

Or use global url function

echo url()->url();

Or use current function

echo url()->current();

Get current full url with query string parameters

echo url()->full();

Get previous URL in Laravel

Sometime we need to check what was the last requested or previous url, it is simple to do in laravel here is how

echo url()->previous();

Current URL is Equal to given String

To evaluate or hide or show something depending on the requested we need to match that the current url is equal to given string we can do that using is function from the request facade.

    if (request()->is('user-profile')) {
        //
    }

Current URL Contains given Pattern

We can also check url using particular pattern like if you want to check that url matches to certain pattern product/3939

Here is the example to achieve this using same request facade.

    if (request()->is('product/*')) {
        //
    }

How to Get Route by Name

If you need to check the name of the current route we can do that use Route class, it is helpful when you don’t want to play with full url or your url might change in future.

\Route::current()->getName();

Match current route using route Name

As we see in the above step we can route by it’s name we can easily check current url using the route name simply do this:

    if(\Route::current()->getName() == 'name-of-your-dsire-route')
    {
        
    }

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

1 year ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

1 year ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

1 year ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

1 year ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

1 year ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

1 year ago