Silex无法找到控制器类

时间:2015-06-08 18:07:48

标签: php controller silex

项目目录结构

/composer.json
/web/index.php
/app/controller/IndexController.php 

composer.json

{
  "require" : {
    "silex/silex": "*"
  },
  "autoload": {
    "psr-0": {
            "": "./"
        }
  }
}

的index.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     

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找不到我的控制器?

3 个答案:

答案 0 :(得分:2)

就我而言,我错过了use Symfony\Component\HttpFoundation\Request;行。

来自this post

  

检查你的控制器是否有这个:

     

使用Silex \ Application;

     

使用Symfony \ Component \ HttpFoundation \ Request;

答案 1 :(得分:1)

试试这个

"autoload" : {
    "psr-4" : {
        "App\\Controller\\" : "app/controller/"
    }
}

答案 2 :(得分:0)

这是一个大写问题。我的macbook有一个不区分大小写的文件系统,我的Linux VPS有一个区分大小写的文件系统,因此在部署到VPS时我必须将项目中的目录大写,以便Silex可以正确解析控制器。