We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 https://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 "https://example.com/api/index?username=test&password=test" what I have to do with Phalcon?

Thank you very much



22.8k
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: https://docs.phalcon.io/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?



22.8k

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 "https://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');
}

https://phalcon-php-framework-documentation.readthedocs.org/en/latest/api/Phalcon_Http_Request.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!