Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

API beside a website

I am new to Phalcon. I have just started working on Phalcon. At this moment I am going through the tutorials. For a project I am going to use Phalcon and in that project beside the website I will have to expose API so that other can use the API. I know the is a option to create Micro app. But my question is what is the best practice in Phalcon to create API with a website as well. Here Model will be same model an website will have the view and API will return the value in json format.



31.7k
Accepted
answer

You must to undestand design API before working it, you can check example on Phalcon or readding ebook Build APIs You Won't Hate

The proper way would be to create a separate module for your API.

But, since you are just starting with Phalcon, you could simply create a separate controller for the API.

Every action will need to return some JSON as a response. This can be done the following way, for example:

$this->view->disable();
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent($whatYouWantToSendAsArray)->send();

First, You should understand what different MVC application and Micro application in Phalcon. I built a system including: Website MVC and REST Api using phalcon. Below is my structure folder and setting, you can see as a suggestion:

  • api
    1. public
    1. rest
    1. app.php
  • config
  • controllers
  • models
  • public
  • views

So, If a request is API => access to api/folder In the api/public/ index.php => Setting as Micro Application (read phalcon's doc)

If a request is website (MVC application) => access to rool/ In the public/ index.php => Setting as MVC Application (read phalcon's doc)

Note: we have total 04 .htaccess: /.htaccess, api/.htaccess, /public/.htaccess, api/public/.htaccess

Hope it's useful.

Thanks

I had tried before the way you mentioned but fail to do so. My request was not going properly to API folder may be I had done something wrong with the htaccess file.

First, You should understand what different MVC application and Micro application in Phalcon. I built a system including: Website MVC and REST Api using phalcon. Below is my structure folder and setting, you can see as a suggestion:

  • api
    1. public
    1. rest
    1. app.php
  • config
  • controllers
  • models
  • public
  • views

So, If a request is API => access to api/folder In the api/public/ index.php => Setting as Micro Application (read phalcon's doc)

If a request is website (MVC application) => access to rool/ In the public/ index.php => Setting as MVC Application (read phalcon's doc)

Note: we have total 04 .htaccess: /.htaccess, api/.htaccess, /public/.htaccess, api/public/.htaccess

Hope it's useful.

edited Feb '15

@sami2019 If you want the request go properly API folder then you can config URL in deploying process

For example:

when request: http://aaaa/ => Website http://aaaa/api123 => API

You should create a link folder (symbolic link) in www/html api123 => link to project/api

Search command "ln"

Hope helpful