Codeigniter RESTful API - fetch data from MySQL database and pass it as JSON

783 views
Skip to first unread message

Steffan Haunstrup Thomsen

unread,
Jan 14, 2014, 7:13:42 AM1/14/14
to codei...@googlegroups.com

I've managed to setup the RESTful API in my Codeigniter Application. Now I want to get some data from my MySQL database and pass it as JSON so I can use it in my Backbone frontend, so in my Codeigniter models folder I have created a model called category_model.php:


<?php

  Class category_model extends CI_Model {

      var $table_name = 'category';


     function get_all_categories()

     {

       $this->db->select('*');

       $this->db->from($this->table_name);

       return $this->db->get();

     }

 }

?>

Then in the Codeigniter controller-folder I created a category.php file:


<?php


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


 class Category extends REST_Controller {


  function __construct()

    {

        parent::__construct();

        $this->load->model('category_model');

    }


  function category_get()

    {

        $data = $this->category_model->get_all_categories();

        $this->response($data);

    }


 }


?>

Now, when I enter http://localhost/myproejcts/ci/index.php/category/category - I get the error {"status":false,"error":"Unknown method."} ??


what is the issue? Can someone help?

Eduardo Mattos

unread,
Feb 25, 2016, 9:31:44 AM2/25/16
to codeigniter
If your method name is category_get i guess that the URL you should access is http://localhost/myproejcts/ci/index.php/category/category_get
Reply all
Reply to author
Forward
0 new messages