项目目录结构
/composer.json
/web/index.php
/app/controller/IndexController.php
composer.json
{
"require" : {
"silex/silex": "*"
},
"autoload": {
"psr-0": {
"": "./"
}
}
}
index . php
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Silex'Application;
$app = new Application();
$app['debug'] = true;
$app->get('/', 'App''Controller''IndexController::getIndex');
$app->run();
IndexController.php
<?php
namespace App'Controller;
use Silex'Application;
class IndexController
{
public function getIndex(Application $app)
{
return "You got the index, congrats!";
}
}
每当我
cd web/
php -S localhost:8080
curl http://localhost:8080
从项目根目录我得到以下异常
InvalidArgumentException in ControllerResolver.php line 153:
Class "App'Controller'IndexController" does not exist.
in ControllerResolver.php line 153
at ControllerResolver->createController('App'Controller'IndexController::getIndex') in ControllerResolver.php line 80
at ControllerResolver->getController(object(Request)) in HttpKernel.php line 135
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 581
at Application->handle(object(Request)) in Application.php line 558
at Application->run() in index.php line 12
这对我来说毫无意义。这个程序实际上在我的MacBook上运行得很好,但是当我把它部署到我的Ubuntu VPS上时,我得到了上面的异常,尽管在两个环境中运行的代码是相同的。为什么Silex找不到我的控制器?
在我的例子中,我错过了use Symfony'Component'HttpFoundation'Request;
行。
从这篇文章:
检查你的控制器是否有这个:
使用Silex '应用;
使用Symfony ' HttpFoundation ' '组件请求;
try this
"autoload" : {
"psr-4" : {
"App''Controller''" : "app/controller/"
}
}
这是一个大小写问题。我的macbook有一个不区分大小写的文件系统,我的Linux VPS有一个区分大小写的文件系统,所以当部署到VPS时,我不得不在我的项目中大写目录,以便Silex可以正确地解析控制器。