Get Url In Laravel

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


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

Grab just the requested 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


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

Grab a Path Segment


//returns orders or order

You may also like...

Leave a Reply

Your email address will not be published.