我向容器注册了一个控制器,但它似乎不起作用,因为它与正确的位置不匹配。
\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
-
将名称从
AppcontrollersHomeController
到Appcontrollers
更改为webAppcontrollersHomeController.php
-
修改\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();