简体   繁体   中英

pass a static value to a controller from routes in laravel

i would like to pass a static value(variable or a static string) from route definition to a controller. for example if consider the following route:

Route::get('/', [
      'uses' => '\Controllers\FoController@show',
      'as' => 'show'
]);

i want to pass a variable(ex, $var1) to that, and then can access to that in the FoController constructor or other controller methods , so i don't like that variable's value shows in the url. how can i do this? thanks...

as a follow up to @serge answer you can do it also this way just posting maybe it will be useful to someone

Route::get('/','fooController@index')
    ->defaults('id', '3');

Use a closure and call the controller yourself and pass what ever you want..

Route::get('/', function(){
  $controller = app()->make('FoController');
  return $controller->callAction('show', ['foo' => 'bar', 'ping' => 'pong']);
});

you can do like this.

Route::any('/home/createuser/{id}','HomeController@createuser');

you can use {} operator for parameters. in controller.

public function createuser($id){
//here you can use $id
}

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