简体   繁体   English

如何在Laravel 5中获取当前页面的细分?

[英]How can I get segment for current page in Laravel 5?

I have URL like this /project/1 我有这样的URL /project/1

How can I get param 1 我如何获得参数1

I need it variable in another controller for another route.... 我需要在另一个控制器的另一个路径中使用它的变量。

here is examle: 这是例子:

route 1: 路线1:

Route::get('project/{id}',array(
            'as'   => 'projectID',
            'uses' => 'FirstController@someMethod'
));

route 2: 路线2:

Route::post('another/route',array(
            'as'   => 'another',
            'uses' => 'SecondController@anotherMethod'
    ));

I need to get inside anotherMethod id param from project/{id} ... I tried like this return Request::segment(2); 我需要从project/{id}进入anotherMethod id参数...我试图像这样return Request::segment(2); but it will return just segments from this route: another/route ... 但它只会返回此路线的路段: another/route ...

Any solution? 有什么办法吗?

You can try this: 您可以尝试以下方法:

Controller: 控制器:

public function index(Request $request){
        return $request->segment(2); //set the segment number it depends on you
    }
public function someMethod(Request $request)
{
    // If you know the segment number
    $id = $request->segment(2);

    // If you know the parameter name
    $id = $request->route('id');

    // If you only know it's the last segment
    $segments = $request->segments();
    $id = array_pop($segments);
}

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

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