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

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

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

查看所有标签

猜你喜欢:

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

大话设计模式

大话设计模式

程杰 / 清华大学出版社 / 2007-12-1 / 45.00元

本书通篇都是以情景对话的形式,用多个小故事或编程示例来组织讲解GOF(设计模式的经典名著——Design Patterns:Elements of Reusable Object-Oriented Software,中译本名为《设计模式——可复用面向对象软件的基础》的四位作者EIich Gamma、Richard Helm、Ralph Johnson,以及John Vlissides,这四人常被称......一起来看看 《大话设计模式》 这本书的介绍吧!

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

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

Markdown 在线编辑器