Get Url In Laravel

by John H
~1 minute

There are a bunch of ways to grab the url in Laravel. Here are a few of the ways I have found.

Grab whole Url

\URL::current();

//returns http://site.localhost/order/202

Grab just the requested URI

\Route::current()->uri

//returns orders or order/{order_id?}

This method can have some strange outcomes if the route has variables being referenced. For example if you have a route that has a variable like this

Route::get('/order/{order_id?}', function ($order_id = null) {
    return view('order');
});

echo \Route::current()->uri; // spits out 'order/{order_id?}'

Don't forget the beginning backslash '\'

Grab the Path

\Request::path();

// returns orders or order/us-tr01-100006568

Grab a Path Segment

\Request::segment(1);

//returns orders or order			

Related Articles

Adding JS libraries in Laravel

Adding a library in Laravel To add with npm the library needs to be included and compiled with...

John H John H
~1 minute

Blocking Access to a Url route in Apache

This serves a 403 forbidden for any route with blog. RewriteRule ^(.*/)?blog(.*/)?/ - [F,L]...

John H John H
~1 minute

Goal Oriented Approach to Development

Keeping a goal focused approach to development can keep projects on track, weed out frivilous...

John H John H
3 minutes