简体   繁体   English

Silex-在route.php中找不到控制器类

[英]Silex - Controller class not found in route.php

I am building a web app with php silex as the backend. 我正在使用php silex作为后端构建一个Web应用程序。 I have the following directory structure: 我有以下目录结构:

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

The file contents are as follows; 文件内容如下;

pp.php pp.php

<?php

    require_once __DIR__ . '/bootstrap.php';

    $app = new Silex\Application();

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

    return $app;

?>

bootstrap.php bootstrap.php中

<?php

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

?>

SupervisorController.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 routes.php文件

<?php

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

composer.json composer.json

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

I get the following error 我收到以下错误

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

I tried adding "autoload": { "psr-4": { "": "./" } } in composer.json but couldn't make it work 我尝试在composer.json添加"autoload": { "psr-4": { "": "./" } } ,但是无法正常工作

Try to change controllers namespace to Bento\\Supervisor . 尝试将控制器名称空间更改为Bento\\Supervisor I don't think that namespace should contain class name. 我认为命名空间不应包含类名。

namespace Bento\Supervisor;

use Silex\ServiceProviderInterface;
use Silex\Application;

class SupervisorController implements ServiceProviderInterface
{

and add autoload to composer 并将自动加载添加到作曲家

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM