Get last part of current URL in Laravel 5 using Blade
Asked Answered
R

10

37

How to get the last part of the current URL without the / sign, dynamically?

For example:

In www.news.com/foo/bar get bar.

In www.news.com/foo/bar/fun get fun.

Where to put the function or how to implement this in the current view?

Rosabella answered 19/6, 2016 at 12:23 Comment(3)
laravel.com/api/5.2/Illuminate/Contracts/Routing/… and do a regex to get the partLimitless
Thanks, could you give me an example?Rosabella
if you post examle code with what you came up with i will help you.Limitless
A
79

Of course there is always the Laravel way:

request()->segment(count(request()->segments()))
Aerothermodynamics answered 20/6, 2016 at 0:2 Comment(2)
My first thought exactly, one line of code ;-) simple to understandHelminthic
This is the best answerPlagiary
I
46

You can use Laravel's helper function last. Like so:

last(request()->segments())

Intention answered 11/1, 2018 at 9:0 Comment(1)
The Most Beautiful Way...Erythrism
B
29

This is how I did it:

{{ collect(request()->segments())->last() }}
Breeden answered 22/1, 2017 at 1:49 Comment(0)
M
20

Use basename() along with Request::path().

basename(request()->path())

You should be able to call that from anywhere in your code since request() is a global helper function in Laravel and basename() is a standard PHP function which is also available globally.

Methanol answered 22/1, 2017 at 7:39 Comment(0)
L
9

The Route object is the source of the information you want. There are a few ways that you can get the information and most of them involve passing something to your view. I strongly suggest not doing the work within the blade as this is what controller actions are for.

Passing a value to the blade

The easiest way is to make the last part of the route a parameter and pass that value to the view.

// app/Http/routes.php
Route::get('/test/{uri_tail}', function ($uri_tail) {
    return view('example')->with('uri_tail', $uri_tail);
});

// resources/views/example.blade.php
The last part of the route URI is <b>{{ $uri_tail }}</b>.

Avoiding route parameters requires a little more work.

// app/Http/routes.php
Route::get('/test/uri-tail', function (Illuminate\Http\Request $request) {
    $route = $request->route();
    $uri_path = $route->getPath();
    $uri_parts = explode('/', $uri_path);
    $uri_tail = end($uri_parts);

    return view('example2')->with('uri_tail', $uri_tail);
});

// resources/views/example2.blade.php
The last part of the route URI is <b>{{ $uri_tail }}</b>.

Doing it all in the blade using the request helper.

// app/Http/routes.php
Route::get('/test/uri-tail', function () {
    return view('example3');
});

// resources/views/example3.blade.php
The last part of the route URI is <b>{{ array_slice(explode('/', request()->route()->getPath()), -1, 1) }}</b>.
Lebron answered 19/6, 2016 at 15:48 Comment(0)
A
5

Try request()->segment($number) it should give you a segment of the URL.

In your example, it should probably be request()->segment(2) or request()->segment(3) based on the number of segments the URL has.

Andrel answered 19/6, 2016 at 14:10 Comment(0)
A
2

YourControllor:

 use Illuminate\Support\Facades\URL;

file.blade.php:

echo basename(URL::current());

Amour answered 30/3, 2021 at 7:25 Comment(0)
D
1

It was useful for me:

request()->path()

from www.test.site/news

get -> news

Diocesan answered 13/1, 2017 at 11:41 Comment(2)
the simplest solution so farCons
This sometimes returns an extra part of the link: example/blabla instead of blablaBricky
Q
0

I just had the same question. In the meantime Laravel 8. I have summarised all the possibilities I know.

You can test it in your web route:

  1. http(s)://127.0.0.1:8000/bar/foo || baz

  2. http(s)://127.0.0.1:8000/bar/bar1/foo || baz

Route::get('/foo/{lastPart}', function(\Illuminate\Http\Request $request, $lastPart) {
    dd(
        [
            'q' => request()->segment(count(request()->segments())),
            'b' => collect(request()->segments())->last(),
            'c' => basename(request()->path()),
            'd' => substr( strrchr(request()->path(), '/'), 1),
            'e' => $lastPart,
        ]
    )->where('lastPart', 'foo,baz'); // the condition is only to limit

I prefer the variant e).

As @Qevo had already written in his answer. You simply make the last part part of the request. To narrow it down you can put the WHERE condition at the route.

Quinine answered 19/10, 2021 at 15:2 Comment(0)
C
-1

Try with:

{{ array_pop(explode('/',$_SERVER['REQUEST_URI'])) }}

It should work well.

Comp answered 19/6, 2016 at 14:12 Comment(1)
Also add a rtrim to remove the last slash if needed.Comp

© 2022 - 2024 — McMap. All rights reserved.