CodeIgniter Forums
CI and Phil Strugeons REST API - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CI and Phil Strugeons REST API (/showthread.php?tid=24644)

Pages: 1 2


CI and Phil Strugeons REST API - El Forum - 11-15-2009

[eluser]moos3[/eluser]
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:Confusedet_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:

Code:
<?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.ellislab.com/codeigniter/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';



?>



CI and Phil Strugeons REST API - El Forum - 11-16-2009

[eluser]Phil Sturgeon[/eluser]
Which version of CodeIgniter are you running?

Do you have anything funny going on with your Output library? Cause that method exists!

Code:
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.


CI and Phil Strugeons REST API - El Forum - 11-16-2009

[eluser]moos3[/eluser]
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.


CI and Phil Strugeons REST API - El Forum - 11-16-2009

[eluser]moos3[/eluser]
Phil,
I can't seem to get my routing to work, can anyone lend a hand?


CI and Phil Strugeons REST API - El Forum - 11-17-2009

[eluser]Phil Sturgeon[/eluser]
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.


CI and Phil Strugeons REST API - El Forum - 11-17-2009

[eluser]moos3[/eluser]
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.

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.


CI and Phil Strugeons REST API - El Forum - 11-17-2009

[eluser]Phil Sturgeon[/eluser]
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.


CI and Phil Strugeons REST API - El Forum - 11-18-2009

[eluser]moos3[/eluser]
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
Code:
<?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);
    }



CI and Phil Strugeons REST API - El Forum - 11-18-2009

[eluser]Phil Sturgeon[/eluser]
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...


CI and Phil Strugeons REST API - El Forum - 11-18-2009

[eluser]moos3[/eluser]
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?