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 // vijayrana.com.np/blog/i/dashboard Request::fullUrl() // OR @if (Request::path() == 'i/dashboard') // url matches @endif // example url // https://vijayrana.com.np/blog/i/dashboard/post/test-post?para1=hello¶2=world dd(Request::fullUrl()); // output https://vijayrana.com.np/blog/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 // vijayrana.com.np/blog/post/this-is-test-post // vijayrana.com.np/blog/post/this-is-another-test-post // vijayrana.com.np/blog/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 // vijayrana.com.np/blog/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://vijayrana.com.np/blog/i/dashboard/post/test-post?para1=hello¶2=world dd(Request::current()); // OR dd(Request::url()); // output https://vijayrana.com.np/blog/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.