2013-11-26 75 views
-1

我正在開發一個包Laravel和我得到我無法弄清楚如何修復的錯誤:錯誤__construct()必須是接口的一個實例,但沒有給出

參數1傳遞給Cartalini \ Drayman \ Drayman :: __ construct()必須是Cartalini \ Drayman \ Repositories \ UserRepositoryInterface的一個實例,沒有給出,在第10行的/Applications/MAMP/htdocs/l4/app/controllers/HomeController.php中調用,定義

這裏是我的代碼...

namespace Cartalini\Drayman; 

use Cartalini\Drayman\Repositories\UserRepositoryInterface; 

class Drayman 
{ 
protected $user; 

public function __construct(UserRepositoryInterface $user) 
{ 
    $this->user = $user; 
} 

public function deliverBeer() 
{ 
    return $this->user->all(); 
} 

} 

UserRepository ...

namespace Cartalini\Drayman\Repositories; 

class UserRepository implements UserRepositoryInterface 
{ 
public function all() 
{ 
    return User::all(); 
} 

} 

UserRepositoryInterface ...

namespace Cartalini\Drayman\Repositories; 

interface UserRepositoryInterface 
{ 
public function all(); 
} 

服務提供商...

public function register() 
{ 
$this->app->bind('Cartalini\Drayman\Repositories\UserRepositoryInterface', 'Cartalini\Drayman\Repositories\UserRepository'); 
} 

最後我的控制器......

use Cartalini\Drayman\Drayman as Drayman; 

class HomeController extends BaseController { 

public function showWelcome() 
{ 
    $drayman = new Drayman; 
    return $drayman->deliverBeer(); 
} 
} 

任何人都可以幫我調試這個嗎?

+3

您是否嘗試閱讀錯誤訊息? – zerkms

+1

消息說明了這一切。 'Daryamn'需要一個'UserRepositoryInterface'類型的參數,但是你不提供一個參數。 –

+0

關注[this](http://culttt.com/2013/06/24/creating-a-laravel-4-package/)和[this](http://jasonlewis.me/article/laravel-4- development-packages-using-the-workbench)在Laravel開發包的文章,我想你錯過了幾件事情。 –

回答

2

在你showWelcome功能:

public function showWelcome() 
{ 
    // need to pass a UserRepositoryInterface object here: 
    $drayman = new Drayman; 
    return $drayman->deliverBeer(); 
} 

既然你沒有通過一個UserRepositoryInterface對象代碼需要你得到這個錯誤。

+0

這不是'Laravel'的工作方式,它是關於[Laravel包](http://laravel.com/docs/packages),這在理論上是正確的,但不是正確的答案。 –

+0

呃?它不正確?請閱讀錯誤消息@RCV – qwertynl

+0

你知道Laravel的工作原理嗎?檢查我在之前的評論中給出的鏈接。 –

相關問題