php - must be an instance of Tests\ContainerInterface
PHP中文网
PHP中文网 2017-04-11 09:17:06
[PHP讨论组]

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;
}
PHP中文网
PHP中文网

认证0级讲师

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号