Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Slim 3框架-为什么路由不能自动加载控制器文件?_Php_Slim 3 - Fatal编程技术网

PHP Slim 3框架-为什么路由不能自动加载控制器文件?

PHP Slim 3框架-为什么路由不能自动加载控制器文件?,php,slim-3,Php,Slim 3,我向容器注册了一个控制器,但它似乎不起作用,因为它与正确的位置不匹配 \web\index.php <?php require __DIR__ . '/vendor/autoload.php'; // Instantiate the app $app = new \Slim\App(['settings' => ['displayErrorDetails' => true] ]); $app->get('/', 'App\controllers\HomeCo

我向容器注册了一个控制器,但它似乎不起作用,因为它与正确的位置不匹配

\web\index.php

<?php


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


// Instantiate the app
 $app = new \Slim\App(['settings' => ['displayErrorDetails' => true] ]);


$app->get('/', 'App\controllers\HomeController:home'); 


// Run!
$app->run();
<?php
require __DIR__ . '/vendor/autoload.php';
// Instantiate the app
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true] ]);
$container = $app->getContainer();
$container['App\controllers\HomeController'] = function ($c) {
    return new App\controllers\HomeController($c);
};
$app->get('/', 'App\controllers\HomeController:home'); 
// Run!
$app->run();
为什么错了?厚度

将NamePase App\controllers\HomeController更改为App\controllers,并将其更改为\web\App\controllers\HomeController.php

<?php

namespace App\controllers\HomeController; 

class HomeController
{
   protected $container;

   // constructor receives container instance
   public function __construct(ContainerInterface $container) {
       $this->container = $container;
   }

    public function __invoke($request, $response, $args) {
        // your code
        // to access items in the container... $this->container->get('');
        return $response;
   }

   public function home($request, $response, $args) {
        // your code
        // to access items in the container... $this->container->get('');
        return $response;
   }

   public function contact($request, $response, $args) {
        // your code
        // to access items in the container... $this->container->get('');
        return $response;
   }
}
修改\web\index.php

<?php


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


// Instantiate the app
 $app = new \Slim\App(['settings' => ['displayErrorDetails' => true] ]);


$app->get('/', 'App\controllers\HomeController:home'); 


// Run!
$app->run();
<?php
require __DIR__ . '/vendor/autoload.php';
// Instantiate the app
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true] ]);
$container = $app->getContainer();
$container['App\controllers\HomeController'] = function ($c) {
    return new App\controllers\HomeController($c);
};
$app->get('/', 'App\controllers\HomeController:home'); 
// Run!
$app->run();

谢谢你的回复,但错误仍然存在:类型:错误消息:未找到类“App\controllers\HomeController”行:7您需要使用composer dump再次运行自动加载生成器autoloadSlim应该能够创建HomeController类,而无需在依赖项容器中注册它,因为其构造函数只接受一个参数,即,容器实例。问题主要是因为目录结构与PSR-4自动加载不匹配。能否向我们显示composer.json文件内容?请确保添加composer.json配置?我想变得更简单,所以开发变得更复杂。