Sometimes we need to check the current URL of requests in our application. Either we need to check the complete path or a specific string of current URL. It can be done easily using following methods provided with Laravel request
object:
1. Inspecting The Request Path / Route
The "is"
method allows you to verify that the incoming request path matches a given pattern. You may use the * character as a wildcard when utilizing this method:
if ($request->is('admin/*')) {
//
}
so we are checking here if URL
has 'admin' in it.
2. Check route by its name
As you probably know, every route can be assigned to a name, in routes/web.php file it looks something like this:
Route::post('password/confirm', 'Auth\PasswordController@validatePassword')->name('password.validate');
Using the routeIs
method, you may determine if the incoming request has matched a named route:
if ($request->routeIs('password.*')) {
//
}
3.Retrieving The Request URL
To retrieve the full URL for the incoming request you may use the url or fullUrl methods. The url method will return the URL without the query string, while the fullUrl method includes the query string:
$url = $request->url();
$urlWithQueryString = $request->fullUrl();
If you would like to append query string data to the current URL, you may call the fullUrlWithQuery method. This method merges the given array of query string variables with the current query string:
$request->fullUrlWithQuery(['type' => 'address']);
so these are the ways to check and work on current URL
in Laravel.