Yii2 从后端控制器调用 api 方法 [英] Yii2 Call api method from backend controllers
问题描述
我已经根据 Yii Rest API 文档实现了基于高级模板的 API.我想从后端控制器调用 API 方法.可以吗?
谢谢
所以,我终于找到了解决方案.
我的 Yii2 应用程序有高级模板.我已经创建了 api 模块.
所以应用有 3 个端点
- api
- 后台
- 前端
我想从后端或前端调用 api 方法,这并不重要.
所以这里api的主要目标是模块.你可以在这里>
在backend/config/main.php
'modules' =>['api' =>['basePath' =>'@api/modules/v1','类' =>'apimodulesv1Module']],
然后例如
在后端/UserController/indexAction
$res = Yii::$app->runAction('api/user/index');
它是如何工作的.希望对某人有所帮助.
I have implemented API based on advanced template following Yii Rest API documentation. And I want to call API methods from backend controllers. Is it possible to do?
Thanks
So, I finally found a solution.
My Yii2 application has advanced template. I've created api module.
So app has 3 endpoints
- api
- backend
- frontend
And I wanted to call api methods from backend or frontend, It's not important.
So the main goal here that api is the module. You can read about this here
In backend/config/main.php
'modules' => [
'api' => [
'basePath' => '@api/modules/v1',
'class' => 'apimodulesv1Module'
]
],
And then for example
in backend/UserController/indexAction
$res = Yii::$app->runAction('api/user/index');
That how it works. Hope it will help to someone.
这篇关于Yii2 从后端控制器调用 api 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!