简体   繁体   English

Yii2 Rest api调用多种方法

[英]Yii2 Rest api call multiple methods

I am using this piece of code, and try using REST API in yii2. 我正在使用这段代码,并尝试在yii2中使用REST API。 I tried to use two function as you seen in the code snippet. 我试着使用你在代码片段中看到的两个函数。

  1. getAllData getAllData
  2. getSpecificData getSpecificData

     <?php namespace app\\api\\modules\\widgetmodule\\controllers; use yii\\rest\\Controller; class WidgetController extends Controller { public $modelClass = 'app\\models\\DynamicWidget'; public function actions() { return [ 'index' => [ 'class' => 'yii\\rest\\IndexAction', 'modelClass' => $this->modelClass, 'prepareDataProvider' => [$this, 'getAllData'] ], 'view' => [ 'class' => 'yii\\rest\\ViewAction', 'modelClass' => $this->modelClass, 'prepareDataProvider' => [$this, 'getSpecificData'] ], ]; } public function getAllData() { die('get all data'); } public function getSpecificData() { die('get specific data'); } } 

I tried two URLs for the two different methods, 我尝试了两种不同方法的URL,

http://localhost/api/web/widgetmodule/widget/getAllData HTTP://本地主机/ API /网络/ widgetmodule /空间/ getAllData

http://localhost/api/web/widgetmodule/widget/getSpecificData HTTP://本地主机/ API /网络/ widgetmodule /空间/ getSpecificData

But the output will always be like. 但输出总是如此。

Quote get all data 引用获取所有数据

Here is my URL manager code in api.php 这是我在api.php中的URL管理器代码

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['widgetmodule/widget']
                ]
            ],
        ], 
        'db' => $db,
    ],
    'modules' => [
        'widgetmodule' => [
           'class' => 'app\api\modules\widgetmodule\Module',
        ],

So could anyone help me, how to get different outputs with two different methods. 所以任何人都可以帮助我,如何用两种不同的方法获得不同的输出。

It is anyways calling the first method. 无论如何调用第一种方法。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

1- The only 2 actions you did define inside your controller are index and view and because index is rendering getAllData it is why you are getting that output. 1-您在控制器中定义的唯一2个操作是indexview ,因为索引正在渲染getAllData这就是您获取该输出的原因。

2- the view action has no prepareDataProvider attribute. 2- view操作没有prepareDataProvider属性。

maybe you meant something like this instead: 也许你的意思是这样的:

public function actions()
{
    return [
        'index' => [
            'class' => 'yii\rest\IndexAction',
            'modelClass' => $this->modelClass,
            'prepareDataProvider' => [$this, 'getAllData']
        ],
        'view' => [
            'class' => 'yii\rest\ViewAction',
            'modelClass' => $this->modelClass,
        ],
    ];
}

public function getAllData()
{
    // return some dataProvider instance to be used by index
    $modelClass = $this->modelClass;

    return new ActiveDataProvider([
        'query' => $modelClass::find(),
    ]);
}

And that is defining two actions: index and view where index's dataProvider is overriden by a custom function getAllData() . 这就是定义两个动作: indexview ,其中索引的dataProvider被自定义函数getAllData()覆盖。 that requires something similar to those configs: 这需要类似于那些配置:

[
    'class' => 'yii\rest\UrlRule', 
    'controller' => ['widgetmodule/widget'], 
    'patterns' => [
        'GET,HEAD index'  => 'index',
        'GET,HEAD view/{id}' => 'view',
    ]
],

So you can access them within the following endpoints: 因此,您可以在以下端点中访问它们:

http://localhost/api/web/widgetmodule/widget/index
http://localhost/api/web/widgetmodule/widget/view/4

Now if your question is how to add an extra action to that in order to respond to the uri http://localhost/api/web/widgetmodule/widget/getSpecificData then you'll need to add this to your patterns: 现在,如果您的问题是如何添加额外的操作以响应uri http://localhost/api/web/widgetmodule/widget/getSpecificData那么您需要将其添加到您的模式中:

'patterns' => [
    ...
    ...
    'GET getSpecificData'  => 'some-specific-data',
]

and define that action inside your controller as described in official docs by simply adding this: 并按官方文档中的描述在控制器中定义该操作,只需添加以下内容即可:

public function actionSomeSpecificData()
{
    return 'some specific data';
}

First unset default index and view actions, like this: 首先取消设置默认索引和视图操作,如下所示:

public function actions()
{
    $actions = parent::actions();
    // unset default index action for custom our own code
    unset($actions['index']);
    unset($actions['view']);

    return ArrayHelper::merge($actions, [
        'index' => [
            'class' => 'yii\rest\IndexAction',
            'modelClass' => $this->modelClass,
            'prepareDataProvider' => [$this, 'getAllData']
        ],
        'view' => [
            'class' => 'yii\rest\ViewAction',
            'modelClass' => $this->modelClass,
            'prepareDataProvider' => [$this, 'getSpecificData']
        ],
    ];
}

Config routes like this: 这样的配置路线:

[
  'class' => 'yii\rest\UrlRule', 
  'controller' => ['widgetmodule/widget'], 
  'patterns' => [
    'GET,HEAD index'  => 'index',
    'GET,HEAD view/{id}' => 'view',
 ]
],

Then you can call these urls: 然后你可以调用这些网址:

http://localhost/api/web/widgetmodule/widget/index
http://localhost/api/web/widgetmodule/widget/view/4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM