php 如何在 Laravel 5 中访问刀片中的 URL 段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31832819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:39:05  来源:igfitidea点击:

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

phplaravellaravel-5blade

提问by cyber8200

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

我有一个网址: http://localhost:8888/projects/oop/2

I want to access the first segment --> projects

我想访问第一段 --> projects

I've tried

我试过了

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

<?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

任何帮助/建议将不胜感激

回答by Aniket Singh

Try this

尝试这个

{{ Request::segment(1) }}

回答by DoubleJ

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

双大括号是通过 Blade 处理的——不仅仅是普通的 PHP。此语法基本上与计算值相呼应。

{{ Request::segment(1) }} 

回答by Software Developer

BASED ON LARAVEL 5.7 & ABOVE

基于 Laravel 5.7 及以上

To get all segments of current URL:

获取当前 URL 的所有段:

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

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

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

postshttp://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 usersand segment(1) will be posts.

注意:段是从索引 0 开始的数组。数组的第一个元素在 url 的 TLD 部分之后开始。所以在上面的 url 中,segment(0) 是users,segment(1) 是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:

您可能已经注意到,segment 方法仅适用于当前 URL ( url()->current())。所以我设计了一个方法来通过克隆方法来处理以前的 URL segment()

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 !== '';
    }));
}

回答by Muhammad

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

下面是如何通过全局request辅助函数来做到这一点。

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

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

Note:request()returns the object of the Requestclass.

注意:request()返回Request类的对象。

回答by Rahul Gupta

Here is code you can get url segment.

这是您可以获得 url 段的代码。

{{ 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

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