1
我正在使用Slim3,但我遇到註冊依賴項的問題。根據錯誤,我創建的構造函數期望參數1的類型爲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
控制器/ 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');
任何想法?