doudang8824 2018-05-24 13:11
浏览 64

如何在使用codeigniter处理REST API时调用表单操作中的方法名称?

Hello everyone I am working on REST API in Codeigniter but i am surprise in normal codeigniter when we give method name after controller for example(localhost/project_name/controller/user_get) it will get all users from table but while working with REST API in Codeigniter I am getting unknown method error please tell me how to define method name while rest api in form action

below i have written my code

in view page when i want to click button in form it should call method and retrieve all users

<form method="post" action="<?=base_url('index.php/Api/user_get')?>">

    <button>Click Here</button>


</form>

controller code

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH . '/libraries/REST_Controller.php');

class Api extends REST_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }


  public function user_get() {   //this method will get all users from table
            $r = $this->user_model->read();
            $this->response($r);
        }


      public function user_put() { //this method will insert users details 
           $id = $this->uri->segment(3);
            $data = array('name' => $this->input->get('user_name'),
                'pass' => $this->input->get('user_password'),
                'type' => $this->input->get('user_type')
            );
            $r = $this->user_model->update($id, $data);
            $this->response($r);
        }

}

Below code written in model

public function read() {

   $query = $this->db->query("select * from `tbl_user`");

   return $query->result_array();

}

Getting error like this

  • 写回答

3条回答 默认 最新

  • dongqiu9018 2018-05-24 13:18
    关注

    try this :

    function users_get()
    {
        $users = $this->user_model->get_all();
    
        if($users)
        {
            $this->response($users, 200);
        }
    
        else
        {
            $this->response(NULL, 404);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿