我目前正在重构我的Laravel 5项目,以便使用Repository Design Pattern。
我有一个创建的存储库接口和一个存储库类:
interface UserRepositoryInterface {
[...]
}
class UserRepository implements UserRepositoryInterface
{
[...]
}
然后在服务提供者类中添加了必要的绑定:
App::bind('App\Repositories\UserRepositoryInterface','App\Repositories\UserRepository');
并将接口注入到控制器构造函数中:
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
}
这部分工作正常。问题是我需要使用 app \ Console \ Kernel.php 类中的一些存储库方法,在那里我实现了一些计划任务。我尝试以类似的方式注入内核构造函数:
/**
* Create a new console kernel instance.
*/
public function __construct(Application $app, Dispatcher $events, UserRepositoryInterface $userRepository)
{
parent::__construct($app, $events);
$this->userRepository = $userRepository;
}
然而,这种方法不起作用(例如在终端中运行'php artisan tinker'失败)。我收到以下错误:
PHP致命错误:未捕获 Illuminate \ Contracts \ Container \ BindingResolutionException:Target [App \ Repositories \ UserRepositoryInterface]不可实例化 建立[App \ Console \ Kernel]。在D:\ xampp \ htdocs \ budget-and-expe NSE-管理\本地\供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php:752 堆栈跟踪:
0 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(633):
Illuminate \ Container \ Container-> build('App \ Repositorie ...',Array)
1 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(697):
Illuminate \ Container \ Container-> make('App \ Repositorie ...',Array)
2 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(853):
照亮\基金会\应用 - >使( '应用\ Repositorie ...')
3 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(808):
照亮\ C in d:\ XAMPP \ htdocs中\预算和支出管理\本地\供应商\ laravel \框架的\ src \伊录姆 第752行的inate \ Container \ Container.php
致命错误:未捕获 Illuminate \ Contracts \ Container \ BindingResolutionException:Target [App \ Repositories \ UserRepositoryInterface]不可实例化 建立[App \ Console \ Kernel]。在D:\ xampp \ htdocs \ budget-and-expense-m中 anagement \本地\供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php 在第752行
Illuminate \ Contracts \ Container \ BindingResolutionException:Target [App \ Repositories \ UserRepositoryInterface]不可实例化 建立[App \ Console \ Kernel]。在 d:\ XAMPP \ htdocs中\预算和支出管理\本地\供应商 第752行\ laravel \ framework \ src \ Illuminate \ Container \ Container.php
调用堆栈: 0.0006 345568 1. {main}()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ artisan:0 0.0365 1417504 2. Illuminate \ Foundation \ Application-> make()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ artisan:31 0.0365 1417552 3. Illuminate \ Container \ Container-> make()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php:697 0.0367 1417552 4. Illuminate \ Container \ Container-> build()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php:633 0.0367 1417552 5. Illuminate \ Container \ Container-> Illuminate \ Container {closure}() d:\ XAMPP \ htdocs中\预算和支出管理\本地\供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php:735 0.0367 1417576 6. Illuminate \ Foundation \ Application-> make()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php:230 0.0367 1417576 7. Illuminate \ Container \ Container-> make()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php:697 0.0368 1417576 8. Illuminate \ Container \ Container-> build()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php:633 0.0388 1453584 9. Illuminate \ Container \ Container-> getDependencies() d:\ XAMPP \ htdocs中\预算和支出管理\本地\供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php:777 0.0397 1456432 10. Illuminate \ Container \ Container-> resolveClass() d:\ XAMPP \ htdocs中\预算和 - 费用管理\本地\厂商\ laravel \框架\ SRC \照亮\容器\ Container.php:808
我想知道是否可以将存储库接口注入内核,如果是的话,我做错了什么?
答案 0 :(得分:5)
你做错了。我想你直接在闭包中的内核中编写了计划任务。而是将命令逻辑包装在Console commands
中<?php
namespace App\Console\Commands;
use App\Repositories\UserRepository;
use Illuminate\Console\Command;
class MyCoolCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mycool:command';
protected $repository = null;
public function __construct(UserRepository $repository)
{
parent::__construct();
$this->repository = $repository;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// your logic here
}
}
然后在内核中调用它们:
$schedule->command('mycool:command')->daily();
然后,您可以在命令构造函数中单独定义每个命令依赖项,清理内核并编写一些可测试且漂亮的代码:)