Slim3 - 传递给构造函数的参数 1 必须是 Slim\Views\Twig 的实例
Slim3 - Argument 1 passed to constructor must be instance of Slim\Views\Twig
我正在使用 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
controllers/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');
有什么想法吗?
问题是我错误地定义了路线,我完全错过了 Container Resolution
上的部分
它应该是这样的:
$app->post('/signup', 'RegistrationController:register');
我正在使用 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
controllers/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');
有什么想法吗?
问题是我错误地定义了路线,我完全错过了 Container Resolution
上的部分它应该是这样的:
$app->post('/signup', 'RegistrationController:register');