简体   繁体   中英

how can I pass an extra parameter to a controller in laravel

I have this:

Route::get('/product/{id}', 'PagesController@display');

And in my PagesController I have this:

public function display($id) {
    return View::make("details", ["id" => $id, "premium" => $premium]);
}

How can I pass the variable $premium to the controller method without inserting it in the url ? In simple words I don't want this: www.mysite.com/product/false/125 ( false is the value of $premium ) but this: www.mysite.com/product/125 . That's why I have just $id as parameter in the controller method and no also $premium . I want to pass that variable $premium in other way.

I've try some approaches like this one:

Route::get('/product/{id}', 'PagesController@display')->where("premium" => "false");

which didn't work.

You can use a closure route and call the action "manually":

Route::get('/product/{id}', function($id){
    return app('PagesController')->callAction('display', [$id, false]);
});

And

public function display($id, $premium) {
    return View::make("details", ["id" => $id, "premium" => $premium]);
}

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