doucang2831 2014-05-05 13:46
浏览 161

如何在Yii2 RESTful API中使用FK选择和返回数据?

I managed to write the REST API code and it works for the standard actions.

Now, if I want to send more attributes, like url_to_api_action?a=b&c=d&e=f, this does not match any of the standard actions.

I need to search by attributes, using a RESTful API in Yii2.

any ideas?

<?php

namespace api\modules\v1\controllers;

use yiiest\ActiveController;

class UneController extends ActiveController {

    public $modelClass = 'common\models\Une';

}
  • 写回答

3条回答 默认 最新

  • doufu1939 2014-05-07 10:47
    关注

    You can create your own actions inside the controller, and you just need to return the result from Active Record and it will take care of formatting the data.

    public function actionSearch($keyword)
    {
        $result = YourModel::find()
                  ->where(['keyword' => $keyword])
                  ->all();
        return $result;
    }
    

    More details here: http://www.yiiframework.com/doc-2.0/guide-rest.html#creating-controllers-and-actions

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!