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

Phalcon REST API Architecture

Hi,

What is the best way to create a REST API with Phalcon ? I have a phalcon application and a phalcon Micro application(who content the REST web services for android application). Should I create a Multi module apps like this ? In this case the web application and the Micro module have the access to the database and it is not necessary to call the web services in the application controller to do a query in database and they have access to the same public folder.

Or should I split the micro module and the applcation ? In this case I have the micro module on a server and the application in another server. Then in my Phalcon controller I have to call the micro module to get the webservices to create / insert / delete into the database because this application doesn't have the database access. And all files created will be on the server of the micro module.

So... What is the best way ?

Thanks.



4.1k
Accepted
answer
edited Apr '16

Create a module for REST API and other for Web Application seems right to me but depends on many factors, server, visits, etc.

For me i prefer multi module (not large application) :

apps
    - REST API
    - Frontend
    - Backend
    - Forum

Phalcon have a powerful Multi Module system, use it.

Thanks for your answer.

With this architecture can I use the same models for the REST API and Web Application or I must duplicate the models in each models folder ?

edited Apr '16

You can do this:

apps
    - REST API
    - Frontend
    - Backend
    - Forum
    - Common (Models/Views/Etc)

https://github.com/phalcon/mvc Here have multiple examples.

See multiple-service-layer-model

Thanks for your answer.

With this architecture can I use the same models for the REST API and Web Application or I must duplicate the models in each models folder ?

edited Apr '16

Wow nice, this is very good.

I have a last question about multi apps. How can I access to Frontend and Backend ? For example I have to call this URL : https://example.com/frontend ? And https://example.com/backend for the backend ?

Edit : There is a way to do https://example.com/ for the frontend and keep the folder architecture ?

You can do this:

apps
  - REST API
  - Frontend
  - Backend
  - Forum
  - Common (Models/Views/Etc)

https://github.com/phalcon/mvc Here have multiple examples.

See multiple-service-layer-model

Thanks for your answer.

With this architecture can I use the same models for the REST API and Web Application or I must duplicate the models in each models folder ?

$di->set('router', function () {

    $router = new Router();

    $router->setDefaultModule("frontend");

    $router->add(
        "/login",
        array(
            'module'     => 'backend',
            'controller' => 'login',
            'action'     => 'index'
        )
    );

    $router->add(
        "/admin/products/:action",
        array(
            'module'     => 'backend',
            'controller' => 'products',
            'action'     => 1
        )
    );

    $router->add(
        "/products/:action",
        array(
            'controller' => 'products',
            'action'     => 1
        )
    );

    return $router;
});

Wow nice, this is very good.

I have a last question about multi apps. How can I access to Frontend and Backend ? For example I have to call this URL : https://example.com/frontend ? And https://example.com/backend for the backend ?

Edit : There is a way to do https://example.com/ for the frontend and keep the folder architecture ?

You can do this:

apps
 - REST API
 - Frontend
 - Backend
 - Forum
 - Common (Models/Views/Etc)

https://github.com/phalcon/mvc Here have multiple examples.

See multiple-service-layer-model

Thanks for your answer.

With this architecture can I use the same models for the REST API and Web Application or I must duplicate the models in each models folder ?

Great :). Thanks for your help

$di->set('router', function () {

   $router = new Router();

   $router->setDefaultModule("frontend");

   $router->add(
       "/login",
       array(
           'module'     => 'backend',
           'controller' => 'login',
           'action'     => 'index'
       )
   );

   $router->add(
       "/admin/products/:action",
       array(
           'module'     => 'backend',
           'controller' => 'products',
           'action'     => 1
       )
   );

   $router->add(
       "/products/:action",
       array(
           'controller' => 'products',
           'action'     => 1
       )
   );

   return $router;
});

Wow nice, this is very good.

I have a last question about multi apps. How can I access to Frontend and Backend ? For example I have to call this URL : https://example.com/frontend ? And https://example.com/backend for the backend ?

Edit : There is a way to do https://example.com/ for the frontend and keep the folder architecture ?

You can do this:

apps
- REST API
- Frontend
- Backend
- Forum
- Common (Models/Views/Etc)

https://github.com/phalcon/mvc Here have multiple examples.

See multiple-service-layer-model

Thanks for your answer.

With this architecture can I use the same models for the REST API and Web Application or I must duplicate the models in each models folder ?

Hey I noticed something. There is no .htaccess with multi modules ?