2017-05-04 1 views
1

내 laravel 컨트롤러 클래스에서 메서드 오버로드 기능을 사용하려고합니다. 여기 내 방법Laravel 컨트롤러에서 메서드 오버로딩을 어떻게 사용합니까?

다음
# 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', '[email protected]'); 

    Route::get('Customer/Balance/{ID}', '[email protected]'); 

그러나 오류 표시 -

FatalErrorException in CustomerController.php line 45: 
Cannot redeclare App\Http\Controllers\CustomerController::createBalance() 

모든 솔루션하시기 바랍니다가?

+2

수 없습니다. PHP는 그렇게 구축되지 않았습니다. – aynber

+1

https://softwareengineering.stackexchange.com/questions/165467/why-php-doesnt-support-function-overloading – aynber

+0

PHP OOP 언어가 아닙니까? 메소드 오버로딩 기능을 가진 OOP 언어의 기능. –

답변

3

사용의 기본 매개 변수를 고려하십시오

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?}', '[email protected]'); 

추가 인수 뒤에이 있으면 Laravel은 선택적 매개 변수를 전달하고 있음을 알고 있습니다.

+1

감사합니다 루이즈 :). 비록 내가 이미 내 임무를 함수의 주장을 세었지만 두 번의 경로 호출을 써야만했다. 너에게 따라, 그것은 나를 한 줄 구해 줬다. –

1

다른 메소드 이름이 필요합니다. 이것은 기본 라우팅 규칙을 따르지 않습니다. 첫 번째 createBalance 메소드는 아마 index 메소드 여야합니다.

1

CustomerController에는 createBalance() 메서드 alredy가 있으므로 새 메서드 또는 다른 메서드를 만들고 새 메서드 이름을 사용하여 경로 파일을 변경해야한다고 생각합니다.

관련 문제