Search code examples
phprestcodeigniterrestful-authenticationion-auth

Using RESTfull with Codeigniter framework


I'm trying to build mobile app for the web page which is implemented entirely in PHP Codeigniter framework. I figured I would reuse website's server API for the mobile app. For that I would need RESTfull services.

The thing is I don't know PHP at all! And from what I gather, websites built on Codeigniter don't normally have public endpoint to invoke it's API. One example would be Ion-auth. The Authentication is done on PHP level with no REST services.

So my question is...does what I've written above make any sense and I understood the framework correctly? If so, does that mean, I'll have to write separate server for my mobile app which would have exactly the same functions and database, or maybe is there a way to reuse current services elegantly?

I have researched about codeigniter-restserver but it seems I would have to rewrite all the logic in the website and I would rather do it in spring or node.


Solution

  • You're going to get a pretty subjective group of answers for this. There are a couple ways to do what you seem to be looking for.

    First, is a separate "API" controller with the CodeIgniter-restserver which duplicates all of your controller functions. This would be sub-optimal since you'll have to maintain your website's code in two places.

    Another, slightly better option would be to check for $this->input->is_ajax_request() or something similar sent by the mobile app, then send responses back with the restserver but the usual view() for the website.

    Ultimately, and your best option, would be to refactor all your code making a single codebase API endpoint (this is where you'd have to decide if CI is a reasonable API or if you want to use something else) and your website and mobile apps use it.

    Edit

    I just realized you specifically mentioned ion-auth, and I happen to have PyroCMS's users module open.

    if ($this->input->is_ajax_request())
            {
                $user = $this->ion_auth->get_user_by_email($user->email);
                $user->password = '';
                $user->salt = '';
    
                exit(json_encode(array('status' => true, 'message' => lang('user:logged_in'), 'data' => $user)));
            }
    

    so, for the restserver you could instead send back:

    $this->response(array('status' => true, 'message' => lang('user:logged_in'), 'data' => $user)), 200);
    

    Edit 2 Regarding how to send post data with Ajax, that's really a whole different question from the OP, but a quick example using jQuery:

    // Not complete code
    <script>
     // Attach a submit handler to the form
     $( "#searchForm" ).submit(function( event ) {
    
      // Stop form from submitting normally
      event.preventDefault();
    
     // Get some values from elements on the page:
     var $form = $( this ),
       term = $form.find( "input[name='s']" ).val(),
       url = $form.attr( "action" );
    
      // Send the data using post
      var posting = $.post( url, { s: term } );
    
     // Put the results in a div
     posting.done(function( data ) {
       var content = $( data ).find( "#content" );
       $( "#result" ).empty().append( content );
      });
    });
    </script>
    

    Example from https://api.jquery.com/jquery.post/