Silex - 在route.php

时间:2016-02-19 09:40:06

标签: php silex autoloader

我正在使用php silex作为后端构建一个Web应用程序。 我有以下目录结构:

bento
  \app
     \app.php
     \bootstrap.php
  \src
     \supervisor
         \SupervisorController.php
      \Application.php
      \routes.php
  \composer.json
  \vendor

文件内容如下;

pp.php

<?php

    require_once __DIR__ . '/bootstrap.php';

    $app = new Silex\Application();

    require_once __DIR__ . '/../src/routes.php';

    return $app;

?>

bootstrap.php中

<?php

    require_once __DIR__ . "/../vendor/autoload.php";

?>

SupervisorController.php

<?php

namespace Bento\Supervisor\SupervisorController;

use Silex\ServiceProviderInterface;
use Silex\Application;

class SupervisorController implements ServiceProviderInterface
{
    public function register(Application $app) {

        $controllers = $app['controllers_factory'];
        $app->get('/supervisor/processes', function() use ($app){

        });

        return $controllers;
    }

    public function boot(Application $app)
    {
        // TODO: Implement boot() method.
    }
}

routes.php文件

<?php

$app->mount('/supervisor', new \Bento\Supervisor\SupervisorController\SupervisorController());

composer.json

{
  "require": {
    "silex/silex": "~1.3"
  }
}

我收到以下错误

PHP Fatal error:  Class 'Bento\Supervisor\SupervisorController\SupervisorController' not found in /somedir/Documents/projects/mv2/bento/src/routes.php on line 3

我尝试在"autoload": { "psr-4": { "": "./" } }中添加composer.json但无法使其正常工作

1 个答案:

答案 0 :(得分:1)

尝试将控制器名称空间更改为Bento\Supervisor。我不认为名称空间应该包含类名。

namespace Bento\Supervisor;

use Silex\ServiceProviderInterface;
use Silex\Application;

class SupervisorController implements ServiceProviderInterface
{

并将自动加载添加到编辑器

"autoload": { "psr-4": { "Bento\Supervisor": "./src/supervisor" } }