laravel 错误 __construct() 必须是接口的一个实例,没有给出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20227858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error __construct() must be an instance of Interface, none given
提问by bencarter78
I'm developing a package for Laravel and I'm getting an error which I can't figure out how to fix:
我正在为 Laravel 开发一个包,但遇到了一个我无法解决的错误:
Argument 1 passed to Cartalini\Drayman\Drayman::__construct() must be an instance of Cartalini\Drayman\Repositories\UserRepositoryInterface, none given, called in /Applications/MAMP/htdocs/l4/app/controllers/HomeController.php on line 10 and defined
传递给 Cartalini\Drayman\Drayman::__construct() 的参数 1 必须是 Cartalini\Drayman\Repositories\UserRepositoryInterface 的实例,没有给出,在 /Applications/MAMP/htdocs/l4/app/controllers/HomeController.php 中调用10 并定义
Here's my code...
这是我的代码...
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();
}
Service provider...
服务提供者...
public function register()
{
$this->app->bind('Cartalini\Drayman\Repositories\UserRepositoryInterface', 'Cartalini\Drayman\Repositories\UserRepository');
}
And finally my controller...
最后我的控制器...
use Cartalini\Drayman\Drayman as Drayman;
class HomeController extends BaseController
{
public function showWelcome()
{
$drayman = new Drayman;
return $drayman->deliverBeer();
}
}
Can anyone help me to debug this please?
任何人都可以帮我调试这个吗?
回答by qwertynl
In your showWelcome
function:
在您的showWelcome
功能中:
public function showWelcome()
{
// need to pass a UserRepositoryInterface object here:
$drayman = new Drayman;
return $drayman->deliverBeer();
}
Since you did not pass a UserRepositoryInterface
object that your code requiresyou get that error.
由于您没有传递UserRepositoryInterface
代码要求的对象,因此您会收到该错误。