我使用的是Slim3,但我在注册依赖项时遇到了问题。根据错误,我创建的构造函数需要参数1的类型为Slim\Views\Twig
。
问题是我正在传递Slim\Views\Twig
的实例 - 至少我认为我是。我在几个月内没有使用过Slim,所以我可能会遗漏一些明显的东西。从来没有我能找到问题。
我得到的错误是:
Catchable fatal error: Argument 1 passed to App\Controllers\RegistrationController::__construct() must be an instance of Slim\Views\Twig, instance of Slim\Container given
控制器/ RegistrationController.php
<?php
namespace App\Controllers;
class RegistrationController {
protected $view;
public function __construct(\Slim\Views\Twig $view) {
$this->view = $view;
}
public function register($request, $response, $args) {
// Does some stuff ...
}
}
dependencies.php
<?php
use \App\Controllers\RegistrationController;
$container = $app->getContainer();
// Twig View
$container['view'] = function ($c) {
$settings = $c->get('settings')['renderer'];
$view = new \Slim\Views\Twig($settings['template_path']);
$basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
return $view;
};
// monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
return $logger;
};
// sqlite3
$container['db'] = function ($c) {
return new SQLite3(__DIR__ . '/../db/prod.db');
};
// Registration Controller
$container['RegistrationController'] = function($c) {
return new RegistrationController($c->get('view'));
};
路线
$app->post('/signup', '\App\Controllers\RegistrationController:register');
还尝试了以下内容:
$app->post('/signup', \App\Controllers\RegistrationController::class . ':register');
有什么想法吗?
答案 0 :(得分:3)
问题是我错误地定义了路线,我完全错过了Container Resolution
上的部分以下是它的外观:
$app->post('/signup', 'RegistrationController:register');