0

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.

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

5 Comments

alright, but how do I know how to direct ajax request towards the server from my app? furthermore, how do I know how POST method should look exactly?
Codeigniter says is_ajax_request() "Checks to see if the HTTP_X_REQUESTED_WITH server header has been set, and returns boolean TRUE if it is or FALSE if not." $_Post data (sent by ajax) is accessed the same with CI for Ajax or "regular" form submission using $this->input->post() On kinda cheat way to do it too is look for something set in the post data like: if ($this->input->post('from_mobile') == 1) {...} and not worry about HTTP Headers... Good place to read up on the Input class: codeigniter.com/user_guide/libraries/input.html
see Edit 2 for sending ajax requests.
I know how to send post requests, what I don't know is how message body should look =] i mean username, password, "rembmerme" and whatever other data must be supplied
As far as getting form values server side, it's the same old same old. $this->input->post('field_name') or $this->post('field_name') (if using the restserver). For sending the form values in ajax take a look here: stackoverflow.com/questions/7426085/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.