我正在使用Laravel 5.2,并希望在我的控制器中访问URL段。我正在使用
echo Request::segment(2);
但是什么都没有被打印。我怎样在控制器中从url获取值。
在 Laravel 5.2 中,你可以这样做...
echo request()->segment(2);
request() 是 Laravel 5.2 提供的几个辅助函数之一,它返回当前请求对象,因此您无需在类顶部使用门面的 use 语句。
$request->segment(3)
获取 URL 数据。 - Mr.Happypublic 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"
}
Illuminate\Http\Request
门面。如果你在使用其他的。 - Ali$request->segment(2)
。 - Jeemusu