在 中找不到自动加载 PSR-4 命名空间接口


{ 
"autoload": {
"psr-4": {
"Acme\": "src"
}
}
}

我的作曲家.json文件

namespace Acme;
Interface Responds
{   
public function userRegisteredSuccessfuly();
public function userRegisteredunSuccessfuly();  
}

界面位于 C:\xampp\htdocs\bootcamp\src\respondstouserregistration.php

<?php 
namespace Acme;
class RegisterUser
{
public function execute(array $data, Responds $listener) //data 
{
var_dump('registering the user.');
$listener->userRegisteredSuccessfuly();
}
}

在我引用的 RegisterUser 中,接口响应,注册用户也位于 src 中

<?php 
namespace Acme;
class AuthController implements Responds
{
protected $registration;
public function __construct(RegisterUser $registration)
{
$this->registration = $registration;
}
public function register()
{
$this->registration->execute([], $this);
}
public function userRegisteredSuccessfuly()
{
var_dump('created successfuly. redirect somewhere.');
}

public function userRegisteredunSuccessfuly()
{
var_dump('created unsuccessfuly. redirect back.');
}
}

在这个类中,我实现了响应接口。

AuthController 也位于 C:\xampp\htdocs\bootcamp\src\authcontroller.php

现在我的错误是

PHP 致命错误:在 C:\xampp\htdocs\bootcamp\src\authcontroller 中找不到接口"Acme\Responds.php 如何使身份验证控制器找到接口所在的 php 文件?

PSR-4 自动加载假定您的类名与文件名匹配。因此,您的Interface Responds应位于responds.php文件中,而不是respondstouserregistration.php文件中。重命名文件,或将接口重命名为Interface Respondstouserregistration

最新更新