PHP Slim 3 Framework-为什么路由不自动加载控制器文件



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

\web\index.php

<?php

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

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

$app->get('/', 'AppcontrollersHomeController:home'); 

// Run!
$app->run();

\web\App/controllers\HomeController.php

<?php
namespace AppcontrollersHomeController; 
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;
}
}

因为它显示瘦应用程序错误:

精简应用程序错误由于以下错误,应用程序无法运行:

详细信息类型:RuntimeException消息:可调用的应用程序\控制器\家庭控制器不存在文件:/Users/feikeq/Desktop/vender/slim/slim/CallableResolver.php线路:90

我的项目文件夹结构:

web 
  index.php
App 
    controllers 
      HomeController.php 
  vendor 
  

为什么它错了?Thk

  1. 将名称从AppcontrollersHomeControllerAppcontrollers更改为webAppcontrollersHomeController.php

  2. 修改\web\index.php

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

最新更新