Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将接口注入Laravels Kernel.php?_Php_Laravel_Dependency Injection_Laravel 5_Repository Pattern - Fatal编程技术网

是否可以将接口注入Laravels Kernel.php?

是否可以将接口注入Laravels Kernel.php?,php,laravel,dependency-injection,laravel-5,repository-pattern,Php,Laravel,Dependency Injection,Laravel 5,Repository Pattern,我目前正在重构我的Laravel5项目,以便利用 我创建了一个存储库接口和一个存储库类: interface UserRepositoryInterface { [...] } class UserRepository implements UserRepositoryInterface { [...] } 然后在服务提供者类中添加了必要的绑定: App::bind('App\Repositories\UserRepositoryInterface','App\Reposito

我目前正在重构我的Laravel5项目,以便利用

我创建了一个存储库接口和一个存储库类:

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致命错误:未捕获 Illumb\Contracts\Container\BindingResolutionException:目标 [App\Repositories\UserRepositoryInterface]在 正在生成[App\Console\Kernel]。在D:\xampp\htdocs\budget and expe中 nse管理\local\vendor\laravel\framework\src\illumb\Container\Container.php:752 堆栈跟踪:

0 D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php(633): 照亮\Container\Container->build('App\Repositorie…',数组)

1d:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\illumb\Foundation\Application.php(697): 照亮\Container\Container->make('App\Repositorie…',数组)

2d:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php(853): 照亮\Foundation\Application->make('App\Repositorie…'))

3d:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php(808): 照亮\C英寸 D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illum 第752行的inate\Container\Container.php

致命错误:未捕获 Illumb\Contracts\Container\BindingResolutionException:目标 [App\Repositories\UserRepositoryInterface]在 正在生成[App\Console\Kernel]。在D:\xampp\htdocs\budget-and-expense-m中 management\local\vendor\laravel\framework\src\light\Container\Container.php 在线752

Illumb\Contracts\Container\BindingResolutionException:目标 [App\Repositories\UserRepositoryInterface]在 正在生成[App\Console\Kernel]。在里面 D:\xampp\htdocs\budget and expense management\local\vendor \第752行的laravel\framework\src\illumb\Container\Container.php

调用堆栈: 0.0006 345568 1. {main}()D:\xampp\htdocs\budget and expense management\local\artisan:0 0.0365 1417504 2. Illumb\Foundation\Application->make()D:\xampp\htdocs\budget and expense management\local\artisan:31 0.0365 1417552 3. Illumb\Container\Container->make()D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illumb\Foundation\Application.php:697 0.0367 1417552 4. Illumb\Container\Container->build()D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illumb\Container\Container.php:633 0.0367 1417552 5. 照亮\Container\Container->照亮\Container{closure}() D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php:735 0.0367 1417576 6. Illumb\Foundation\Application->make()D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illumb\Container\Container.php:230 0.0367 1417576 7. Illumb\Container\Container->make()D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illumb\Foundation\Application.php:697 0.0368 1417576 8. Illumb\Container\Container->build()D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\Illumb\Container\Container.php:633 0.0388 1453584 9. 照亮\Container\Container->getDependencies() D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php:777 0.0397 1456432 10. 照亮\Container\Container->resolveClass() D:\xampp\htdocs\budget and expense management\local\vendor\laravel\framework\src\light\Container\Container.php:808


我想知道是否可以将存储库接口注入内核,如果可以,我做错了什么?

您做错了。我想您是直接在内核中的闭包中编写计划任务的。相反,将您的命令封装在逻辑中


然后,您可以在命令构造函数中分别定义每个命令依赖项,清理内核并编写一些可测试且漂亮的代码:)

我不知道这是否可行,但是
$userRepository=\App::make('App\Repositories\UserRepositoryInterface')
应该可以正常工作,并且基本上具有相同的结果(虽然DI可能更干净、更容易测试,但到底是谁在测试内核?)虽然这个答案与原来的问题不同,但这里您没有使用依赖项注入。您直接引用了存储库。我遇到了同样的问题,我想做的是在命令文件中使用UserRepositoryInterface,并将其自动绑定到存储库,就像我在con中所做的一样特罗勒,这可能吗?
<?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();