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.

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.



3.2k
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.



13.9k

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 ?



13.9k
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 : http://example.com/frontend ? And http://example.com/backend for the backend ?

Edit : There is a way to do http://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 : http://example.com/frontend ? And http://example.com/backend for the backend ?

Edit : There is a way to do http://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 ?



13.9k

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 : http://example.com/frontend ? And http://example.com/backend for the backend ?

Edit : There is a way to do http://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 ?



13.9k

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