How to access URL segment(s) in blade in Laravel 5?
Asked Answered
I

6

61

I have a url : http://localhost:8888/projects/oop/2

I want to access the first segment --> projects

I've tried

<?php echo $segment1 = Request::segment(1); ?>

I see nothing print out in my view when I refresh my page.


Any helps / suggestions will be much appreciated

Isreal answered 5/8, 2015 at 12:39 Comment(3)
did you use Request namaspace?Cliquish
Yes. Should I declare it somewhere ? How do I do that ?Isreal
@ImtiazPabel : Should I include this on the top use Request;Isreal
W
131

Try this

{{ Request::segment(1) }}
Whereon answered 5/8, 2015 at 12:40 Comment(2)
How is it diff than what I have ? Just out of curiosity.Isreal
@ihue it's not. Blade will internally convert "{{" into "<?php echo" and "}}" into ";?>". So it's exactly the same as what you are doing, and will work just the same.Rosner
I
13

BASED ON LARAVEL 5.7 & ABOVE

To get all segments of current URL:

$current_uri = request()->segments();

To get segment posts from http://example.com/users/posts/latest/

NOTE: Segments are an array that starts at index 0. The first element of array starts after the TLD part of the url. So in the above url, segment(0) will be users and segment(1) will be posts.

//get segment 0
$segment_users = request()->segment(0); //returns 'users'
//get segment 1
$segment_posts = request()->segment(1); //returns 'posts'

You may have noted that the segment method only works with the current URL ( url()->current() ). So I designed a method to work with previous URL too by cloning the segment() method:

public function index()
{
    $prev_uri_segments = $this->prev_segments(url()->previous());
}

 /**
 * Get all of the segments for the previous uri.
 *
 * @return array
 */
public function prev_segments($uri)
{
    $segments = explode('/', str_replace(''.url('').'', '', $uri));

    return array_values(array_filter($segments, function ($value) {
        return $value !== '';
    }));
}
Injector answered 17/10, 2019 at 10:36 Comment(1)
This is not true (at least not anymore for Laravel 8.47)! segments() returns a 0-based array, but if you call segment() the indexing is 1-based, so segment(1) will be "users" and segment(2) will be "posts". In typical Laravel style, this is not mentioned at all in the documentation, but the doc comment of the segment() method "Get a segment from the URI (1 based index)." confirms this.Ezekielezell
I
11

The double curly brackets are processed via Blade -- not just plain PHP. This syntax basically echos the calculated value.

{{ Request::segment(1) }} 
Inge answered 13/2, 2017 at 8:11 Comment(0)
S
6

Here is how one can do it via the global request helper function.

{{ request()->segment(1) }}

Note: request() returns the object of the Request class.

Sweated answered 15/6, 2019 at 14:24 Comment(0)
H
6

An easy way to get the first or last segment, in case you are unsure of the path length.

$segments = request()->segments();
$last  = end($segments);
$first = reset($segments);
Honeyman answered 10/7, 2020 at 15:30 Comment(0)
P
1

Here is code you can get url segment.

{{ Request::segment(1) }}

If you don't want the data to be escaped then use {!! !!} else use {{ }}.

{!! Request::segment(1) !!}

https://laravel.com/docs/4.2/requests

Pelt answered 17/10, 2019 at 11:28 Comment(1)
Can you give an example of when you would want to use the not escaped version? Also, be very careful with this! It is very easy for users with malicious intent to play with the url if you do.Hypercatalectic

© 2022 - 2024 — McMap. All rights reserved.