php - Codeigniter Restful API not working -
i have codeigniter setup installed restful api thing. created api-folder in application->controller->api , after created api looks this:
<?php require(apppath.'libraries/rest_controller.php'); class allartists extends rest_controller{ function artists_get() { if(!$this->get('artist_id')) { $this->response(null, 400); } $artists = $this->artist_model->get( $this->get('artist_id') ); if($artists) { $this->response($artists, 200); } else { $this->response(array('error' => 'couldn\'t find artists!'), 404); } } ?> in application->models-folder have file artist_model.php looks this:
<?php class artist_model extends ci_model { function get_all_artists(){ $this->db->select('*'); $this->db->from('artists'); return $this->db->get(); } } ?> so, when type http://localhost/myprojects/ci/index.php/api/allartists/artists/ 400 - bad request-error... when type http://localhost/myprojects/ci/index.php/api/allartists/artists/artist_id/100 php error undefined property: allartists::$artist_model - going on here?
you need load model. add constructor allartists , load it.
class allartists extends rest_controller{ function __construct(){ parent::__construct(); $this->load->model('artist_model'); } // ... } p.s. model needs have 1st letter in class name capitalized (see: http://ellislab.com/codeigniter/user-guide/general/models.html):
class artist_model extends ci_model{ // ... } update: looking $this->get('artist_id'). never set not sending $_get['artist_id'] value (?artist_id=100 in url). need $artist_id way in controller.
function artists_get($artist_id=false) { if($artist_id === false) { $this->response(null, 400); } $artists = $this->artist_model->get( $artist_id ); if($artists) { $this->response($artists, 200); } else { $this->response(array('error' => 'couldn\'t find artists!'), 404); } } then go to:
http://localhost/myprojects/ci/index.php/api/allartists/artists/100 or, keeping current code, change url to:
http://localhost/myprojects/ci/index.php/api/allartists/artists?artist_id=100
Comments
Post a Comment