How to access url segment in controller in laravel 5.2

只谈情不闲聊 提交于 2021-01-27 05:41:39

问题


I am working in Laravel 5.2 and i want to access URL segments in my controller. I am using

echo  Request::segment(2);

but nothing is print. How can i get values from url in controller.


回答1:


In laravel 5.2 you can do it this way..

echo request()->segment(2);

request() is one of the several helper functions provided in 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.




回答2:


In Laravel 7, I am using this to get segments

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"
}


来源:https://stackoverflow.com/questions/37695512/how-to-access-url-segment-in-controller-in-laravel-5-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!