【问题标题】:Yii Rest api url managerYii Rest api url 管理器
【发布时间】:2014-10-27 11:36:59
【问题描述】:

我无法找到 url 管理器背后的逻辑。任何人都可以告诉我有关在 Rest 模式中调用的给定 url。我的控制器中有太多带有单个参数的 get 方法。

这是我的原始网址:localhost/project/api/event/getevent/event_id/1

api 是模块 事件是控制器 getevent 是控制器动作名称 event_id 是参数1

我想将其转换为 Rest 模式 localhst/project/api/event/1

//更新代码..

所有都与同一个模型相关.......

    //Search by Event id
    public function actionByEventId(){

        $model->byEventId();
    }


    //Retrieve user's events by User id
    public function actionByUserId()
    {  

       $model->userEvents();  
    }

   //Search for event by code
    public function actionByEventcode()
    {      

        $model->byEventCode(); 

    }

【问题讨论】:

  • 你能添加你想怎么称呼他们吗?喜欢localhst/project/api/event/1localhst/project/api/user/1
  • 不,我想要这样的 localhst/project/api/event/eventid/1 和 localhst/project/api/event/userid/1

标签: api url yii


【解决方案1】:

添加这条规则:

'api/<controller:\w+>/<action:\w+>/<id:\d+>' => 'api/<controller>/by<action>'

并添加$id您的操作:

public function actionByEventId($id) {

}
public function actionByUserId($id) {

}

现在,如果你调用localhst/project/api/event/eventid/1,Yii 会在你的event-Controller 中调用actionByEventId$id 为 1

【讨论】:

  • 感谢您的回复。我试过你的代码。它适用于单一方法。我有一个参数的多个操作。在这里,我遇到了问题。来自 api 的任何东西,都需要作为 ID。所以我不能调用多个动作。你能指导我吗?
  • @Kishore 请在您的问题中添加第二个示例,我可以看到您的结构是什么样的
  • 嗨,更新了主帖。在同一个控制器中添加了 3 个具有单个 GET 参数的功能.. 我该如何调用这些 api..
  • 感谢您的回复。当我调用上述 url 时,eventid 和 userdid 正在作为操作。出现错误,例如无法解决此问题..我没有操作名称..就像那样。但这两个是参数...
  • @Kishore:你不能同时运行 2 个动作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多