1, 在slim3.0中, 当使用传统方法调用时, 参数传递错误
2, 报错信息: Catchable fatal error: Argument 1 passed to TestsMyController::__construct() must be an instance of TestsContainerInterface, instance of SlimContainer given, called in D:phpStudyWWWslim3.0vendorslimslimSlimCallableResolver.php on line 64
3, 代码如下:
<?php
namespace Tests;
class MyController {
protected $ci;
//Constructor
public function __construct(ContainerInterface $ci) {
$this->ci = $ci;
}
public function method1($request, $response, $args) {
//echo $args["name"];
//your code
//to access items in the container... $this->ci->get('');
}
}
?>
3, 路由调用:$app->get('/method1','TestsMyController:method1');
4, 尝试了下面的方法, 仍然不能解决
http://stackoverflow.com/questions/37906363/slim-controller-issue-must-be-an-instance-of-containerinterface-instance-of-s?answertab=active#tab-top
Option 1:
Import the namespace in your index.php, just like it is being done for Request and Response:
use \Interop\Container\ContainerInterface as ContainerInterface;
Option 2:
Change the constructor of the TopPageController to:
public function __construct(Interop\Container\ContainerInterface $ci) {
$this->ci = $ci;
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
认证0级讲师