我正在使用本教程学习依赖注入的工作原理,直到此时,一切都按预期工作。但是添加路由后,它开始抛出异常。我跳到下一步,甚至尝试从Github下载源代码,但问题仍然存在。这是我收到的异常:
PHP Fatal error: Uncaught DI\\Definition\\Exception\\InvalidDefinition: Entry "ExampleApp\\HelloWorld" cannot be resolved: the class doesn't exist
Full definition:
Object (
class = #UNKNOWN# ExampleApp\\HelloWorld
lazy = false
) in /var/www/html/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:18
Stack trace:
#0 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(120): DI\\Definition\\Exception\\InvalidDefinition::create(Object(DI\\Definition\\ObjectDefinition), 'Entry "ExampleA...')
#1 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(71): DI\\Definition\\Resolver\\ObjectCreator->createInstance(Object(DI\\Definition\\ObjectDefinition), Array)
#2 /var/www/html/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(64): DI\\Definition\\Resolver\\ObjectCreator->resolve(Object(DI\\Definition\\ObjectDefinition), Array)
#3 /var/www/html/vendor/php-di/php-di/src/Container.php(380): DI\\Definit in /var/www/html/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 18
这是我的 apache 配置:
<IfModule mod_ssl.c>
<VirtualHost 10.0.7.2:443>
ServerAlias xyz.com
DocumentRoot /var/www/html/public
ServerName xyz.com
<Directory "/var/www/html/public">
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [QSA,L]
</Directory>
</VirtualHost>
</IfModule>
(不想有 .htaccess 文件,我可以在我的服务器上更改 httpd.conf 文件)。
这是 public/index.php 文件:
declare(strict_types=1);
use DI\ContainerBuilder;
use ExampleApp\HelloWorld;
use FastRoute\RouteCollector;
use Middlewares\FastRoute;
use Middlewares\RequestHandler;
use Narrowspark\HttpEmitter\SapiEmitter;
use Relay\Relay;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use function DI\create;
use function DI\get;
use function FastRoute\simpleDispatcher;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions([
HelloWorld::class => create(HelloWorld::class)
->constructor(get('Foo'), get('Response')),
'Foo' => 'bar',
'Response' => function() {
return new Response();
},
]);
/** @noinspection PhpUnhandledExceptionInspection */
$container = $containerBuilder->build();
$routes = simpleDispatcher(function (RouteCollector $r) {
$r->get('/hello', HelloWorld::class);
});
$middlewareQueue[] = new FastRoute($routes);
$middlewareQueue[] = new RequestHandler($container);
/** @noinspection PhpUnhandledExceptionInspection */
$requestHandler = new Relay($middlewareQueue);
$response = $requestHandler->handle(ServerRequestFactory::fromGlobals());
$emitter = new SapiEmitter();
/** @noinspection PhpVoidFunctionResultUsedInspection */
return $emitter->emit($response);
这是/src/HelloWorld.php:
namespace ExampleApp;
use Psr\Http\Message\ResponseInterface;
/**
* @package ExampleApp
*/
class HelloWorld
{
private $foo;
private $response;
/**
* @param ResponseInterface $response
* @param string $foo
*/
public function __construct(
string $foo,
ResponseInterface $response
) {
$this->foo = $foo;
$this->response = $response;
}
/**
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function __invoke(): ResponseInterface
{
$response = $this->response->withHeader('Content-Type', 'text/html');
$response->getBody()
->write("<html><head></head><body>Hello, {$this->foo} world!</body></html>");
return $response;
}
}
这是我的 composer.json 文件:
{
"name": "myapp/platform",
"description": "My test platform",
"type": "project",
"license": "Copyright",
"require": {
"php-di/php-di": "^6.0",
"relay/relay": "^2.0",
"zendframework/zend-diactoros": "^2.2",
"middlewares/fast-route": "^1.2",
"middlewares/request-handler": "^1.4",
"narrowspark/http-emitter": "^1.0"
},
"autoload": {
"psr-4": {
"MineApp\\": "src/"
}
}
}
我的配置有什么问题?我该如何解决?