如何在使用codeigniter处理REST API时调用表单操作中的方法名称? [英] How to call method name in form action while working with REST API using codeigniter?

查看:80
本文介绍了如何在使用codeigniter处理REST API时调用表单操作中的方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在使用Codeigniter中的REST API,但是当我们在控制器之后给出方法名称(例如localhost / project_name / controller / user_get)时,我会对普通的codeigniter感到惊讶它会从表中获取所有用户但是在使用REST时Codeigniter中的API我得到了未知的方法错误请告诉我如何在表格动作中使用rest api时定义方法名称



下面我写了我的代码



在查看页面中,当我想点击表单中的按钮时,它应该调用方法并检索所有用户



我尝试过:



 <  表格   方法  =  post   行动  = <?= base_url('index.php / Api / user_get')?> >  

< 按钮 > 点击此处< / button >


< / form >





控制器代码

<?php

定义('BASEPATH')或退出('不允许直接访问脚本');

require(APPPATH。'/ libs/REST_Controller.php');

类Api扩展REST_Controller {

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


公共函数user_get(){//此方法将从表中获取所有用户
$ r = $ this-> user_model-> read ();
$ this-> response($ r);
}


公共函数user_put(){//此方法将插入用户详细信息
$ 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);
}

}





以下代码是用型号写的

公共函数read(){



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

返回$ query-> result_array();
}



我收到的错误如上所述

 {status: false,error:Unknown method} 



这个图片获取错误的链接。



https://i.stack.imgur.com/ykktL.png

解决方案

this-> load-> model('user_model');
}


公共函数user_get(){//此方法将从表中获取所有用户


r =


这 - > user_model->读();

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 working with 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

What I have tried:

<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 is written in model

public function read() {



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

       return $query->result_array();
   }


I am getting error like this mentioned

{"status":false,"error":"Unknown method"}


this the link for image getting error.

https://i.stack.imgur.com/ykktL.png

解决方案

this->load->model('user_model'); } public function user_get() { //this method will get all users from table


r =


this->user_model->read();


这篇关于如何在使用codeigniter处理REST API时调用表单操作中的方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆