Route.php can't find controller class I created

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 05:44:07

问题


I'm trying to build a silex application. My file structure is:

ROOT/
    /App/
        /Controller/{IndexController.php}
        /Config/{dev.php,prod.php,route.php}
    /vendor
    /web/{index.php, index_dev.php}

When I'm trying to see http://localhost/web/ I get error:

PHP Fatal error: Class 'App\Controller\IndexController' not found in ../App/config/route.php on line 2

Here are the relevant files:

index_dev.php

<?php

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

require __DIR__.'/../App/config/dev.php';
$app = require __DIR__.'/../App/app.php';

$app->run();

?>

app.php

<?php

use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$app = new Application();

require __DIR__.'/config/route.php';
return $app;

?>

route.php

<?php

$app->mount('/', new App\Controller\IndexController());

?>

IndexController.php

<?php

namespace App\Controller;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;

class IndexController implements ControllerProviderInterface {

  public function index(Application $app) {
    return phpinfo();
  }

  public function connect(Application $app) {
    $controllers = $app['controllers_factory'];

    $app->get('/', 'App\Controller\IndexController::index');

    return $controllers;
  }

}

?>

composer.json

{
    "require": {
        "silex/silex": "1.0.*"
    },
    "minimum-stability": "dev"
}

回答1:


You are missing autoloder options in composer.json:

"autoload": { "psr-0": { "App": "./" } }


来源:https://stackoverflow.com/questions/13200941/route-php-cant-find-controller-class-i-created

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!