CI and Phil Strugeons REST API
Posted: 15 November 2009 06:48 PM
[ Ignore ]
Summer Student
Total Posts: 11
Joined 09-16-2009
I’m trying to integrate it in my application and I’m getting the following using phils latest commits.
Fatal error: Call to undefined method CI_Output::set_status_header() in /var/www/system/application/libraries/REST_Controller.php on line 97
can anyone Help with this matter? I also had to comment out my whole routes configuration to get it to please see below:
<?php if (! defined ( 'BASEPATH' )) exit( 'No direct script access allowed' ); /* | ------------------------------------------------------------------------- | URI ROUTING | ------------------------------------------------------------------------- | This file lets you re-map URI requests to specific controller functions. | | Typically there is a one-to-one relationship between a URL string | and its corresponding controller class/method. The segments in a | URL normally follow this pattern: | | www.your-site.com/class/method/id/ | | In some instances, however, you may want to remap this relationship | so that a different class/function is called than the one | corresponding to the URL. | | Please see the user guide for complete details: | | http://www.codeigniter.com/user_guide/general/routing.html | | ------------------------------------------------------------------------- | RESERVED ROUTES | ------------------------------------------------------------------------- | | There are two reserved routes: | | $route['default_controller'] = 'welcome'; | | This route indicates which controller class should be loaded if the | URI contains no data. In the above example, the "welcome" class | would be loaded. | | $route['scaffolding_trigger'] = 'scaffolding'; | | This route lets you set a "secret" word that will trigger the | scaffolding feature for added security. Note: Scaffolding must be | enabled in the controller in which you intend to use it. The reserved | routes must come before any wildcard or regular expression routes. | */ $route[ 'default_controller' ] = "main" ; $route[ 'scaffolding_trigger' ] = "" ; //$route['system/:any'] = "main/cron"; //$route['clip/raw/:any'] = 'main/raw/'; //$route['clip/options'] = 'main/view_options'; //$route['clip/:any'] = 'main/view'; //$route['recent'] = 'main/recent'; //$route['recent/:num'] = 'main/recent/$1'; //$route['about'] = 'main/about'; //$route['clip/download/:any'] = 'main/download'; //$route['iphone/:num'] = 'iphone'; //$route['iphone/view/:any'] = 'iphone/view'; //$route['api/:any'] = 'api'; //$route['example_api/:any'] = 'example_api'; ?>
Posted: 16 November 2009 06:53 AM
[ Ignore ]
[ # 1 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
Which version of CodeIgniter are you running?
Do you have anything funny going on with your Output library? Cause that method exists!
function set_status_header ( $code = '200' , $text = '' )
If you are using 1.7.x and you have no modifications to your Output library, try editing REST_Controller on line 97 and use the function set_status_header() over the output class method.
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 16 November 2009 08:51 AM
[ Ignore ]
[ # 2 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
using the 1.7.2 download for CI, I pulled down you package, and it works, but when I moved it in to my application it didn’t work, so I’m writing mine based off your application download, it all seems to work accept my route for api to point to example_api just for testing doesn’t work, but I’m just going to write up my api file and see what happens. Will post my results. Thanks phil for getting back to me so quick.
Posted: 16 November 2009 01:39 PM
[ Ignore ]
[ # 3 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
Phil,
I can’t seem to get my routing to work, can anyone lend a hand?
Posted: 17 November 2009 10:44 AM
[ Ignore ]
[ # 4 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
Sorry buddy my home internet is screwed at home and I have no set-up anywhere else. Somebody will need to hop in here if they can.
I’ll have a go off-line tonight if I have time.
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 17 November 2009 03:49 PM
[ Ignore ]
[ # 5 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
Phil,
I have the example working. now trying to figure out the how the controller knows how to route stuff. I have created the following funciton in it but when I call /api/pastes/lists/format/xml it giving me a 404 below is my code.
<?php require( APPPATH . '/libraries/REST_Controller.php' ); class Api extends REST_Controller { function get_lists () { $this -> load -> model ( 'languages' ); $data = $this -> pastes -> getLists (); if( $data ) { $this -> response ( $data , 200 ); }else{ $this -> response ( NULL , 404 ); } } function paste_get () { if(! $this -> get ( 'id' )) { $this -> response ( NULL , 404 ); } $this -> load -> model ( 'languages' ); $check = $this -> pastes -> checkPaste ( 'id' ); if( $check ) { $data = $this -> pastes -> getPaste ( 'id' , true ); }else{ $this -> response ( NULL , 404 ); } if( $data ) { $this -> response ( $data , 200 ); }else{ $this -> response ( NULL , 404 ); } } } ?>
any help would be great, I have never done rest before so I’m trying to figure out this.
Posted: 17 November 2009 07:23 PM
[ Ignore ]
[ # 6 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
I’m viewing this on an iPhone so excuse me if I’m missing something, but looms to me like your controllers methods are named wrong. Remember that there is a remap function that appends the HTTP verb onto the end of the method, so /api/paste would be paste_get() via a browser call. Post, put and delete are all done with something like curl.
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 18 November 2009 10:58 AM
[ Ignore ]
[ # 7 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
Here is my controller. But if I’m understanding you correctly so /api/paste/pastes/ will automagically go to pastes_get()?
heres the pastes controller
<?php class Main extends Controller { function __construct () { parent :: __construct (); $this -> load -> model ( 'languages' ); } function _form_prep ( $lang = 'php' , $title = '' , $paste = '' , $reply = false ) { $this -> load -> model ( 'languages' ); $this -> load -> helper ( "form" ); $data[ 'languages' ] = $this -> languages -> get_languages (); $data[ 'scripts' ] = array( 'jquery.js' , 'jquery.timers.js' ); if(! $this -> input -> post ( 'submit' )) { if( $this -> db_session -> flashdata ( 'settings_changed' )) { $data[ 'status_message' ] = 'Settings successfully changed' ; } $data[ 'name_set' ] = $this -> db_session -> userdata ( 'name' ); $data[ 'expire_set' ] = $this -> db_session -> userdata ( 'expire' ); $data[ 'acopy_set' ] = $this -> db_session -> userdata ( 'acopy' ); $data[ 'private_set' ] = $this -> db_session -> userdata ( 'private' ); $data[ 'snipurl_set' ] = $this -> db_session -> userdata ( 'snipurl' ); $data[ 'remember_set' ] = $this -> db_session -> userdata ( 'remember' ); $data[ 'paste_set' ] = $paste ; $data[ 'title_set' ] = $title ; $data[ 'reply' ] = $reply ; if( $lang != 'php' or ( $lang == 'php' and $this -> db_session -> userdata ( 'lang' ) == false )) { $data[ 'lang_set' ] = $lang ; } elseif( $this -> db_session -> userdata ( 'lang' )) { $data[ 'lang_set' ] = $this -> db_session -> userdata ( 'lang' ); } } else { $data[ 'name_set' ] = $this -> input -> post ( 'name' ); $data[ 'expire_set' ] = $this -> input -> post ( 'expire' ); $data[ 'acopy_set' ] = $this -> input -> post ( 'acopy' ); $data[ 'private_set' ] = $this -> input -> post ( 'private' ); $data[ 'snipurl_set' ] = $this -> input -> post ( 'snipurl' ); $data[ 'remember_set' ] = $this -> input -> post ( 'remember' ); $data[ 'paste_set' ] = $this -> input -> post ( 'paste' ); $data[ 'title_set' ] = $this -> input -> post ( 'title' ); $data[ 'reply' ] = $this -> input -> post ( 'reply' ); $data[ 'lang_set' ] = $this -> input -> post ( 'lang' ); } return $data ; } function index () { if(!isset( $_POST[ 'submit' ] )) { $data = $this -> _form_prep (); $this -> load -> view ( 'home' , $data ); } else { $this -> load -> model ( 'pastes' ); $this -> load -> library ( 'validation' ); $rules[ 'code' ] = 'required' ; $rules[ 'lang' ] = 'min_length[1]|required|callback__valid_lang' ; $fields[ 'code' ] = 'Main Paste' ; $fields[ 'lang' ] = 'Language' ; $this -> validation -> set_rules ( $rules ); $this -> validation -> set_fields ( $fields ); $this -> validation -> set_message ( 'min_length' , 'The %s field can not be empty' ); $this -> validation -> set_error_delimiters ( '<div class="message error"><div class="container">' , '</div></div>' ); if ( $this -> validation -> run () == FALSE ) { $data = $this -> _form_prep (); $this -> load -> view ( 'home' , $data ); } else { if(isset( $_POST[ 'acopy' ] ) and $_POST[ 'acopy' ] > 0 ) { $this -> db_session -> set_flashdata ( 'acopy' , 'true' ); } if( $this -> input -> post ( 'remember' ) and $this -> input -> post ( 'reply' ) == false ) { $user_data = array( 'name' => $this -> input -> post ( 'name' ), 'lang' => $this -> input -> post ( 'lang' ), 'expire' => $this -> input -> post ( 'expire' ), 'acopy' => $this -> input -> post ( 'acopy' ), 'snipurl' => $this -> input -> post ( 'snipurl' ), 'private' => $this -> input -> post ( 'private' ), 'remember' => $this -> input -> post ( 'remember' ) ); $this -> db_session -> set_userdata ( $user_data ); } if( $this -> input -> post ( 'remember' ) == false and $this -> db_session -> userdata ( "remember" ) == 1 ) { $user_data = array( 'name' => '' , 'lang' => 'php' , 'expire' => '0' , 'acopy' => '0' , 'snipurl' => '0' , 'private' => '0' , 'remember' => '0' ); $this -> db_session -> unset_userdata ( $user_data ); } redirect ( $this -> pastes -> createPaste ()); } } } function lists () { $this -> load -> model ( 'pastes' ); $data = $this -> pastes -> getLists (); $this -> load -> view ( 'list' , $data ); }
Posted: 18 November 2009 11:24 AM
[ Ignore ]
[ # 8 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
That controller is nothing to do with my RESTful implementation. You are not including or extending REST_Controller so it will not act like a REST controller…
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 18 November 2009 12:35 PM
[ Ignore ]
[ # 9 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
So what should i do to make that controller usable via the api rest controller that I have start. Can you just give me a starting point?
Posted: 18 November 2009 12:50 PM
[ Ignore ]
[ # 10 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
Look at the example_api.php. It does everything you need to get a REST controller working.
You cannot mix REST and normal Controllers, they are different things.
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 18 November 2009 02:09 PM
[ Ignore ]
[ # 11 ]
Summer Student
Total Posts: 11
Joined 09-16-2009
Posted: 04 February 2010 05:28 AM
[ Ignore ]
[ # 12 ]
Grad Student
Total Posts: 35
Joined 01-14-2010
Ok phil, i have installed and tried your RESTimplementation and it looks very promising, i am new to RESTful webservices and get the
http : //localhost/codeigniter/index.php/example_api/user/id/1
and
http : //localhost/codeigniter/index.php/example_api/users/users/format/html
to work
but how do i add a user? how do i use rest to start a function (for example a counter of users in the database? and return the result?)
i am trying to make a simple restful server that
1. you can insert single users from a aspx application and 2. return the amount of users in the database
perhaps some more documentation on how to make restful services? I could help you do this if you help me understand how it works
or perhaps a tutorial with models and a database for practical applications?
would love your help. thank you for your library and efforts.
Posted: 04 February 2010 06:06 AM
[ Ignore ]
[ # 13 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
I have no idea how to do it in ASP.NET as I am masochistic enough to work with that language, but essentially you need to post to:
http://localhost/codeigniter/index.php/example_api/user
And create a function user_post() in the controller. Then just use $this->post(‘first_name’) and interact with your CodeIgniter models however you normally would.
There should be a NetTuts tutorial posted about all this today but they are slacking. They said it would be out already. :-(
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management
Posted: 04 February 2010 06:13 AM
[ Ignore ]
[ # 14 ]
Grad Student
Total Posts: 35
Joined 01-14-2010
ok thanks you dont have to explain the .net side, other peeps will solve that perhaps i have missed out on how to post to the REST service? do you recommend an application or specific method to actually test the rest service?
thanks for the heads up on the nettuts i will check it out :D will probably spam you here and on nettuts nuts until i get a hang of it
Posted: 04 February 2010 06:23 AM
[ Ignore ]
[ # 15 ]
Sr. Research Associate
Total Posts: 3175
Joined 06-11-2007
Use cURL or something similar. That is what my CodeIgniter REST Client uses.
Signature
————————
Blog | Twitter | GitHub | BitBucket ————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management