使用Blade获取Laravel 5中当前URL的最后一部分

栏目: 编程语言 · PHP · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/37907191/get-last-part-of-current-url-in-laravel-5-using-blade

如何动态获取当前URL的最后一部分没有’/’?

例如:

www.news.com/foo/bar

得到 – >酒吧

www.news.com/foo/bar/fun

得到 – >开玩笑

在当前视图中放置函数或如何实现它的位置?

Route object

是您想要的信息的来源.您可以通过几种方式获取信息,其中大部分都涉及将某些内容传递给您的视图.我强烈建议不要在刀片中进行工作,因为这是控制器操作的用途.

将值传递给刀片

最简单的方法是使路由的最后一部分成为参数,并将该值传递给视图.

// 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>.

避免路由参数需要更多的工作.

// 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>.

使用 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>.

翻译自:https://stackoverflow.com/questions/37907191/get-last-part-of-current-url-in-laravel-5-using-blade


以上所述就是小编给大家介绍的《使用Blade获取Laravel 5中当前URL的最后一部分》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

TED Talks Storytelling: 23 Storytelling Techniques from the Best

TED Talks Storytelling: 23 Storytelling Techniques from the Best

Akash Karia / CreateSpace Independent Publishing Platform / 2015-1-11 / USD 6.99

"Every speaker can put these ideas into practice immediately -- and they should!" ~ Dr. Richard C. Harris, Certified World Class Speaking Coach "An insightful read" ~Dennis Waller, Top 500 Revie......一起来看看 《TED Talks Storytelling: 23 Storytelling Techniques from the Best》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具