How to check current URL in Laravel 5

How to check current URL in Laravel 5

When working with the web application, we often need to check if the current URL matches some conditions. To check current URL in blade or controller, laravel provides some helper functions which makes our work easier.

We can access current url using Laravel’s Request and URL facade. Here, we will access our url’s using different approaches in laravel 5.

Get full URL

This will give the complete url with the base url. We can get full url of active web address using the below syntax. Please note that this will get the url with parameters

Request::fullUrl()

// output
http://127.0.0.1:8000/i/dashboard

Get current url

We can get the current url i.e. url after base url using the path() method.

// example url
// kodementor.com/i/dashboard

Request::fullUrl()
// OR
@if (Request::path() == 'i/dashboard')
    // url matches
@endif

// example url
// https://kodementor.com/i/dashboard/post/test-post?para1=hello¶2=world
dd(Request::fullUrl());

// output
https://kodementor.com/i/dashboard/post/test-post?para1=hello¶2=world

Compate if current URL match certain pattern

This is really useful and often need a lot when working with the blade. Suppose, when we need to check if the URL is for displaying posts. So, the basic format of URL will be base-url/post/slug-of-each-post.

In such case, slug is unique for all post. So, we check pattern as /post/* to compare such conditions.

    // example urls
    // kodementor.com/post/this-is-test-post
    // kodementor.com/post/this-is-another-test-post
    // kodementor.com/post/this-is-nex-another-test-post

    @if (Request::is('post/*'))
        // condition matches
    @endif

Note
The is() method accepts multiple arguments. So, you can write multiple conditions to check.

    @if (Request::is('post/*', 'some-string', 'another-string'))
        // Any of the given string/condition matches
        // wite your login
    @endif

Get segment of current url

When we have to check for the segments of url, then segment function comes as a role play. Unlike to matching whole pattern, it checks for specific segment of url.

    // example urls
    // kodementor.com/dashboard/post/this-is-test-post

    @if (Request::segment(4) == 'some-text')
        // Note:
        // segment(1)  == 'dashboard'
        // segment(2)  == 'post'
        // segment(3)  == 'this-is-test-post'
        // and so on
    @endif

Get route name

We sometimes need to check the name of route rather than url itself. We can also get the route name using getName() function of route. The basic syntax for this is:

// example url
// Route::get('i/dashboard/index', 'DashController')->name('my-dashboard')

@if (Request::route()->getName() == 'my-dashboard')
    // this is the exact url
@endif

Note
This above Request::route()->getName() function is similar to Route::is('').

Request::route()->getName()
// this is similar to
Route::is('')

Get current url without parameters

We can get the url without parameters unlike fullUrl using current() method or url() method.

// example url
// https://kodementor.com/i/dashboard/post/test-post?para1=hello¶2=world
dd(Request::current());
// OR
dd(Request::url());

// output
https://kodementor.com/i/dashboard/post/test-post

This is all for checking URL in laravel. If you find any problem in implementating the code, please don’t forget to drop a comment below.

Read More Articles

4 COMMENTS

  1. My brother recommended I might like this website.
    He was totally right. This post truly made my day.

    You cann’t imagine just how much time I had spent for this information! Thanks!

  2. These wonderfl devices work like a camera, a mini computer,
    a MP3 player and a gamig consoile whenever required.
    Due to its high specs, gaming laptops usually are far more expensive compared to regular laptops and often much chunkier too.

    Eventually, electronics manufacturers started taking into consideration the probabilities of automating domestic
    microcontroller based devices.

  3. I’m really inspired together with your writing abilities
    as well as with the structure in your weblog.
    Is that this a paid theme or did you modify it your
    self? Anyway keep up the nice high quality writing,
    it is uncommon to look a nice weblog like this one these days.

LEAVE A REPLY

Please enter your comment!
Please enter your name here