为什么我的laravel绑定到实现的接口需要composer更新,否则它将无法工作?
当我测试laravel绑定接口到实现时,我得到了一个错误。 我认为这与自动加载有关。你能告诉我为什么吗?告诉我更多细节。 以下是我的步骤: 我制作了一个界面:为什么我的laravel绑定到实现的接口需要composer更新,否则它将无法工作?,laravel,Laravel,当我测试laravel绑定接口到实现时,我得到了一个错误。 我认为这与自动加载有关。你能告诉我为什么吗?告诉我更多细节。 以下是我的步骤: 我制作了一个界面: namespace App\Contracts; interface EventPusherInterface { public function showSelf(); } 并加以实施 namespace App\Services; use App\Contracts\EventPusherInterface; clas
namespace App\Contracts;
interface EventPusherInterface
{
public function showSelf();
}
并加以实施
namespace App\Services;
use App\Contracts\EventPusherInterface;
class RedisEventPusher implements EventPusherInterface
{
public function showSelf()
{
return 'test contact';
}
}
在AppServiceProvider中注册
$this->app->bind(
EventPusherInterface::class,
RedisEventPusher::class
);
并在控制器中询问
namespace App\Http\Controllers;
use App\Contracts\EventPusherInterface;
use App\Contracts\InterFaceTestSecond;
use Illuminate\Http\Request;
class TestContactController extends Controller
{
/**
* @var InterFaceTestSecond
*/
protected $eventPusher;
/**
* TestContactController constructor.
* @param InterFaceTestSecond $eventPusher
*/
public function __construct(
InterFaceTestSecond $eventPusher
) {
$this->eventPusher = $eventPusher;
}
public function show()
{
dd($this->eventPusher->showSelf());
}
}
然后我犯了个错误
然后我命令编写器更新
成功了。
我只是想知道为什么???这是因为Laravel bootstrap的应用程序 如果查看
/vendor/autoload_clasmap.php
,您会发现所有类都映射到了那里。这使得通过服务容器将它们解析为依赖项成为可能
并非所有文件都需要重新生成自动加载程序,因为它们不是通过服务容器解析的。在您的情况下,您的接口和继承模式必须通过服务容器解析,因此依赖于autoload_classmap
来包含这些类的定义
希望这能把事情弄清楚。谢谢。那我怎么解决呢?文档没有告诉我们在这种情况下配置自动加载文件。当
将接口绑定到实现时,是否每次都需要编写器更新?我认为这不是一个好主意。如果我能以某种方式对其进行配置?@Eqer否,您每次都需要composer更新
。我已经对其进行了测试,结果表明我每次创建接口时都需要composer更新,否则我会出错。