2013-11-26 74 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的工作原理吗?检查我在之前的评论中给出的链接。 –

相关问题