来自后端控制器的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',
'class' => 'api\modules\v1\Module'
]
],
然后例如
在后端/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' => 'api\modules\v1\Module'
]
],
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屋!