thinkphp Container容器绑定类

public function index()//要用的时候绑定类到容器
    {
        bind([
                'test' => Test::class
        ]);
        return json(app('test')->index());
    }

  • 在模块下定义provider.php文件,系统会自动绑定这里的类,然后就可以app(‘test’)->index()调用了
<?php
namespace app\api;
return [
    'test'   => \app\api\controller\v1\Test::class
];

下面是Test类

<?php
namespace app\api\controller\v1;
class Test{
    public function index(){
        echo 'test lei';
    }
}
发布了122 篇原创文章 · 获赞 5 · 访问量 4819

猜你喜欢

转载自blog.csdn.net/weixin_41254345/article/details/104630290