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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考