【发布时间】:2018-11-27 09:21:56
【问题描述】:
我在容器中注册了一个控制器,但它似乎无法正常工作,因为它与正确的位置不匹配。
\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();
\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;
}
}
因为它显示 Slim 应用程序错误:
Slim 应用程序错误 由于以下错误,应用程序无法运行:
详情 类型:运行时异常 消息:Callable App\controllers\HomeController 不存在 文件:/Users/feikeq/Desktop/vendor/slim/slim/Slim/CallableResolver.php 线路:90
我的项目文件夹结构:
\web
index.php
\App
\controllers
HomeController.php
\vendor
为什么错了?想
【问题讨论】:
-
你能告诉我们你的
composer.json文件内容吗? -
一定要添加“composer.json”配置吗?我想更简单,所以开发变得更复杂。