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

\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

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *