我目前正在重构我的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致命错误:Uncaught Illuminate \ Contracts \ Container \ BindingResolutionException:目标[App \ Repositories \ UserRepositoryInterface]在构建[App \ Console \ Kernel]时无法实例化 . 在D:\ xampp \ htdocs \ budget-and-expe nse-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ 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 ......' ,数组)2 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(853):Illuminate \ Foundation \ Application-> make(' App \ Repositorie ...')3 D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php(808):在D中照亮\ C :\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illum inate \ Container \ Container.php 752行致命错误:Uncaught Il luminate \ Contracts \ Container \ BindingResolutionException:构建[App \ Console \ Kernel]时,目标[App \ Repositories \ UserRepositoryInterface]不可实例化 . 在D:\ xampp \ htdocs \ budget-and-expense-m anagement \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php第752行Illuminate \ Contracts \ Container \ BindingResolutionException:Target [App \ Repositories构建[App \ Console \ Kernel]时,\ UserRepositoryInterface]不可实例化 . 在D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php第752行调用堆栈:0.0006 345568 1. ()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 ()D:\ xampp \ htdocs \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php: 735 0.0367 1417576 6. Illuminate \ Foundation \ Application-> make()D:\ xampp \ htdocs \ budget-an d-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 \ budget-and-expense-management \ local \ vendor \ laravel \ framework \ src \ Illuminate \ Container \ Container.php:777 0.0397 1456432 10. Illuminate \ Container \ Container-> resolveClass()D:\ xampp \ htdocs \ budget-and-expense-management \ local \供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php:808
我想知道是否可以将存储库接口注入内核,如果是的话,我做错了什么?
1 回答
你做错了 . 我想你直接在闭包中的内核中编写了计划任务 . 相反,包裹你的Console commands中的命令逻辑
然后像这样在内核中调用它们:
然后,您可以在命令构造函数中单独定义每个命令依赖项,清理内核并编写一些可测试且漂亮的代码:)