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

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

内容简介:翻译自: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的最后一部分》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

Algorithms and Data Structures

Algorithms and Data Structures

Kurt Mehlhorn、Peter Sanders / Springer / 2008-08-06 / USD 49.95

Algorithms are at the heart of every nontrivial computer application, and algorithmics is a modern and active area of computer science. Every computer scientist and every professional programmer shoul......一起来看看 《Algorithms and Data Structures》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具