How to use Phalcon PHP to create web service

I have to create a simple android app that has login function with php and mysql. I followed this tutorial http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ and everything is fine.

But problem is the PHP part of this tutorial use plain PHP. Now I want to use PhalconPHP to do that job (because we use Phalcon to create our web client, so I want to use the same technology).

I don't know what is the project structure for a web service with Phalcon, is it similar to MVC web client? And if I want the string query like "http://example.com/api/index?username=test&password=test" what I have to do with Phalcon?

Thank you very much



14.4k
edited Apr '14

you have to make a REST API that returns json data for every request from your android application.

follow this tutorial from Phalcon docs: http://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

I've read that tutorial, but it still hard for me to understand. Do I need a controller or just a index.php?



14.4k

You can make it like the tutorial in the index (if you don't have much functionnalities) or make it in the MVC way without using views here is an example that I did for my app .

Let's say that you have a controller for users that contains actions like (login, signup, register session...):

class UsersController extends \Phalcon\Mvc\Controller{

  //make your content type for response as a json data in the initialize() function
  public function initialize(){

    $this->response->setContentType('application/json; charset=utf-8');
    $this->view->disable();
  }

  loginAction(){

    //some operactions to do ....

    //set the response data in array
    $jsonResult = array(
    'data' =>  //some data that u want to send to the app as a response
    )

    //We set our response json content
    $this->response->setJsonContent($jsonResult);

    //and finally we return the response object so It will be the result for the client automatically.
    return $this->response:
}

As you can see there is no need for views.

I don't know what is the project structure for a web service with Phalcon, is it similar to MVC web client?

Check out https://github.com/cmoore4/phalcon-rest as a starting point.

And if I want the string query like "http://example.com/api/index?username=test&password=test" what I have to do with Phalcon?

You need to access the request object. From inside a controller that would look like:

public function indexAction()
{

    $username = $this->di->getShared('request')->get('username');
    $password = $this->di->getShared('request')->get('password');
}

http://phalcon-php-framework-documentation.readthedocs.org/en/latest/api/Phalcon%5Http%5Request.html

sn0opr : OK, but what my index.php need to be? I need to handle the POST request which has a "tag" (to show what action : login, register, forgot pass,...), "username", "password",....

Thank you!