我在让类自动加载在我正在组装的 Silex 骨架中工作时遇到问题。我在另一个项目中有这个工作,但我不知道我在这里做错了什么。我的目录如下所示:
root
-src
-Controller
-HelloController.php
-app.php
-vendor
-web
-index.php
-composer.json
这是我的 index.php
<?php
$app = require __DIR__.'/../src/app.php';
$app->run();
应用程序.php
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->get("/hello/{name}", 'App\Controller\HelloController::hello');
return $app;
你好控制器.php
<?php
namespace App\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function hello($name)
{
return new Response('<html><head></head><body><h1>Hello, '.$name.'</h1></body></html>');
}
}
和 composer.json
{
"require": {
"silex/silex": "^1.3"
},
"autoload": {
"psr-4": {
"App\\": "/src"
}
}
}
每当我尝试在浏览器中打开 index.php/hello/world 时,都会收到此错误:
InvalidArgumentException in ControllerResolver.php line 153:
Class "App\Controller\HelloController" does not exist