简体   繁体   中英

Pass get parameter to controler in Laravel

How do I pass a get variable to a controller in Laravel?

I have:

$languages = array('zh');
$locale = Request::segment(1);
if (in_array($locale, $languages)) {
    App::setLocale($locale);
} else {
    $locale = null;
}
Route::group(array('prefix' => $locale), function() {
    ...
    Route::get('/search/{q}', array('as' => 'search', 'uses' => 'ProductsController@index'));
    ...
});

If I try to return q from within the controller using Input::get('q'); I get nothing.

Route::get('/search/{q}', array('as' => 'search', 'uses' => 'ProductsController@index'));

the {q} here isn't GET variable.

you can take the value like this.

public function index($q)
{
     echo $q;
}

It's not a GET parameter but an URL parameter. There's a little difference in that.

Url paramters will be passed (in the order they appear) to the controller action.

So all you have to do, is use the argument that gets automatically passed to the function in the controller.

public function index($q){
    echo $q;
}

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