这种方式很简单:
Route::get('user/profile', 'PaymentsController@profile');
Route::get('user/delete', 'PaymentsController@delete');
我想用一行来做这件事:
Route::get('user/{subsection}', 'PaymentsController@'.$subsection);
但我的语法似乎错了。是否可以一行完成?如果可能的话会很好。
答案 0 :(得分:1)
不,你不能这样做,但你可以制作代理方法
Route::get('user/{subsection}', 'PaymentsController@profileDelete');
方法看起来像
public function profileDelete($subsection) {
return $this->$subsection();
}
public function profile(){}
public function delete(){}
此外,您可以绑定{subsection}
Route::bind('subsection', function ($subsection) {
if (!in_array($subsection, ['profile', 'delete'])) {
throw new Exception;
}
return $subsection;
});
答案 1 :(得分:1)
不完全是一行,但你可以这样做:
Route::get('user/{subsection}', function($subsection){
if(!method_exists('PaymentsController', $subsection)){
App::abort(404);
}
return App::make('PaymentsController')->callAction($subsection);
});
除了method_exists
之外,你还可以使用路由条件来仅允许一组预定义的子章节:
Route::get('user/{subsection}', function($subsection){
return App::make('PaymentsController')->callAction($subsection);
})->where('subsection', '(profile|delete)');