[英]Laravel: using url parameter for controller action
sorry i'm a laravel newbie - here's my route code: 抱歉,我是laravel新手-这是我的路线代码:
Route::get('shop/{id}', function($id)
{
});
$id
represents the shop category id and i would like to pass id to a controller-action like shop\\StartController@showarticles
$id
代表商店类别ID,我想将ID传递给类似shop\\StartController@showarticles
的控制器操作
i'm only used to this syntax: 我只习惯了这种语法:
Route::get('/', 'shop\StartController@show');
how would i do that? 我该怎么办? thanks
谢谢
Just pass it as a parameter like this (assuming Laravel 5.1 is used): 只需将其作为这样的参数传递(假设使用Laravel 5.1):
class StartController extends Controller
....
public function show($id)
{
//add controller logic
}
//Asuming that the namespace `shop` is loaded
Route::get('/', 'StartController@show');
you can easily pass the id to controller 您可以轻松地将ID传递给控制器
Route::get('shop/{id}','UserController@show');
Your controller 您的控制器
class UserController extends BaseController {
public function show($id)
{
echo "hi its my first exercise in laravel".$id;
}
You can use closure for your route and call the controller like this: 您可以对路由使用闭包,并按如下所示调用控制器:
Route::get('shop/{id}', function($id)
return App::make('StartController')->showarticles($id);
}));
Your controller would be like 您的控制器会像
Route::get('shop/{id}', 'shop\StartController@showarticles');
and your class function would be like 和你的班级职能将是
class StartController extends Controller
....
public function show($id)
or incase you are using latest version 或者如果您使用的是最新版本
class StartController extends Controller
....
public function show(Request $request, $id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.