How to Get Current URL in Laravel
source link: https://www.laravelcode.com/post/how-to-get-current-url-in-laravel
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Get Current URL in Laravel
In blade files or Controllers, you may want to catch current url or path name. You may sometimes want to change CSS or need to change element based on current route. Laravel provides simple methods to get current urls or path.
Here is few examples you may use to get current route and url data. The methods will automatically use the schemes(http or https) from url.
Get current url without query string.
echo \URL::current() // or
echo url()->current()
// https://laravelcode.com/article/
Get the full url with query string parameters.
echo \URL::full() // or
echo url()->full()
// https://laravelcode.com/article?param=value¶m2=value2
You may sometimes want to get previous url.
echo \URL::previous() // or
echo url()->previous()
// https://laravelcode.com/article/
If you want to get specific segment of url, you may use segment() method with segment number as parameter. segment() will count number from 1.
echo Request::segment(1) // or
echo request()->segment(1) // or
// article
If you want to get name of the route, you may use \Route::currentRouteName() method.
echo \Route::currentRouteName()
echo \Route::current()->getName()
// article.index
You may get the controller path and method name from \Route::currentRouteAction() method
echo \Route::currentRouteAction()
// App\Http\Controllers\ArticleController@index
You may get the route object with \Route::current() object for more details.
Author : Harsukh Makwana
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK