简体   繁体   English

如何在 Laravel 5.2 中访问控制器中的 url 段

[英]How to access url segment in controller in laravel 5.2

I am working in Laravel 5.2 and i want to access URL segments in my controller.我在 Laravel 5.2 中工作,我想访问控制器中的 URL 段。 I am using我正在使用

echo  Request::segment(2);

but nothing is print.但没有什么是印刷品。 How can i get values from url in controller.如何从控制器中的 url 获取值。

In laravel 5.2 you can do it this way..在 Laravel 5.2 中你可以这样做..

echo request()->segment(2);

request() is one of the several helper functions provided in Laravel 5.2. request() 是 Laravel 5.2 中提供的几个辅助函数之一。 It returns the current request object thus you don't need use statement for the facade on the top of your class.它返回当前的请求对象,因此您不需要对类顶部的外观使用 use 语句。

In Laravel 7, I am using this to get segments在 Laravel 7 中,我使用它来获取段

public function my_function(Request $request )
{
    // By using this, we can get the second segment in route
    // Example: example.com/hh/kk
    
    $segment = $request->segment(2);

    // By using this we will get "kk"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM