laravel Lumen:如何将参数从路由传递给控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31282798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen: how to pass parameters to controller from route
提问by jrsalunga
from my route i need to pass the value of $page to controller
从我的路线,我需要将 $page 的值传递给控制器
route:
路线:
$app->get('/show/{page}', function($page) use ($app) {
$controller = $app->make('App\Http\Controllers\PageController');
return $controller->index();
});
controller:
控制器:
public static function index(){
/** how can I get the value of $page form here so i can pass it to the view **/
return view('index')->with('page', $page);
}
采纳答案by Jeemusu
You could pass it as a parameter of the index function.
您可以将其作为索引函数的参数传递。
Route
路线
$app->get('/show/{page}', function($page) use ($app) {
$controller = $app->make('App\Http\Controllers\PageController');
return $controller->index( $page );
});
Although the route looks wrong to me, normally you define the route without a forward slash at the beggining: $app->get('show/{page}', ...
.
尽管路线在我看来是错误的,但通常您定义的路线在开头没有正斜杠: $app->get('show/{page}', ...
。
Controller
控制器
public static function index($page)
{
return view('index')->with('page', $page);
}
Unless there is a reason for using a closure, your route could be re-written as below, and the {$page}
variable will automatically be passed to the controller method as a parameter:
除非有使用闭包的原因,否则你的路由可以重写如下,{$page}
变量将自动作为参数传递给控制器方法:
Route
路线
$app->get('show/{page}', [
'uses' => 'App\Http\Controllers\PageController@index'
]);
回答by Pahrul Irfan
in my case, showing specified user, it's pretty much the same case
在我的情况下,显示指定的用户,情况几乎相同
route file(web.php)
路由文件(web.php)
Route::get('user/{id}/show', ['as'=> 'show', 'uses'=>'UserController@show']);
im still using facade
我还在使用门面
view file(users.blade.php)
查看文件(users.blade.php)
href="{{route('show', ['id' => $user->id])}}"
just passing an array to route name and the last one in
只是传递一个数组来路由名称和最后一个
controller file(UserController.php)
控制器文件(UserController.php)
public function show($id)
{
$user = User::findorfail($id)->first();
return view('user', compact('user'));
}
it's done
完成