在 Silex 中为控制器使用命名空间


Using namespace in Silex for controllers

我正在尝试 Silex 框架,但我遇到了一个问题......我尝试将路由处理程序委托给控制器(作为 SF2)。

但是我有一个 500 错误告诉我找不到控制器类......我试图在类似的问题中寻求帮助,但没有答案适用于我的情况......

这是目录架构:

Application 
|-src   
  |-App   
    |-Controller
      |-IndexController 
|-vendor   
  |-... 
|-web   
  |-index.php
|-composer.json

这是"索引.php":

require_once __dir__.'/../vendor/autoload.php';
use App'Controller;
$app = new Silex'Application();
$app['debug'] = true;
$app->register(new Silex'Provider'ServiceControllerServiceProvider());
$app['index.controller'] = $app->share(function() use ($app) {
    return new IndexController();
});
$app->get('/', 'index.controller:indexAction');
$app->run();

这里是"索引控制器.php"

namespace App'Controller;
class IndexController
{
    public function __construct() {}
    public function indexAction()
    {
        return "Use of controller class : OK";
    }
}

这里是"composer.json"

{
    "require": {
        "silex/silex": "~1.3"
    },
    "autoload": {
        "psr-4": {"App''": "src/App/"}
    }
}

(当然,我做了一个"作曲家更新"来管理作曲家.json文件的更新)

你没有在use中定义IndexController,所以 php 找不到控制器类。
将用途更改为use App'Controller'IndexController;或将新类更改为return new Controller'IndexController();