Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

HTML
<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
Posted
Updated 24-May-18 21:16pm
v2

1 solution

The method refers to the HTTP method. In your form, you use a POST:

<form method="post"


While it should be GET.
 
Share this answer
 
Comments
Msp_md 25-May-18 4:01am    
@Thaddeus Jones i tried that way also but it is not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900