简体   繁体   中英

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

I have URL like this /project/1

How can I get param 1

I need it variable in another controller for another route....

here is examle:

route 1:

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

route 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); but it will return just segments from this 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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