我正在尝试在我的 Laravel Controller 类中使用方法重载功能。这是我的方法
# Load Customer Balance View
public function createBalance()
{
return view('customer.balance');
}
# Load Customer Balance View
public function createBalance($CustomerID)
{
// show balance of the the defined customer
}
这是我的路线-
// customer balance
Route::get('Customer/Balance', 'CustomerController@createBalance');
Route::get('Customer/Balance/{ID}', 'CustomerController@createBalance');
但它显示错误-
FatalErrorException in CustomerController.php line 45:
Cannot redeclare App\Http\Controllers\CustomerController::createBalance()
有什么解决办法吗?
最佳答案
考虑使用默认参数:
public function createBalance($CustomerID=null)
{
if ($CustomerID==null)
return view('customer.balance');
else
// show balance of the the defined customer
}
并将您的路线更改为:
Route::get('Customer/Balance/{CustomerID?}', 'CustomerController@createBalance');
在参数后添加一个 "?",Laravel 知道您正在传递一个可选参数。
关于php - 如何在 Laravel Controller 中使用方法重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786355/