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 Multi module and micro application rest api

Hi,

I'm building a multi module. I have an Application named frontend and a REST API in micro application. But I have a question about the way to create my rest api. It's necessary to use a micro app in multi module ? Because I can't define the routes for my rest api because it has't controller, just an index.php.

Then... It's possible to use micro module to build my rest api in a multi module ? Or I have to build my rest api in a controller class ?

Here is my architecture.

multiple/
├── common
│   ├── config
│   │   ├── config.php
│   │   ├── modules.php
│   │   └── services.php
│   ├── library
│   │   └── Elements.php
│   │   └── Tools.php
│   │   └── PhpMailer.php
│   ├── models
│   │   └── User.php
├── modules
│   ├── frontend
│   │   ├── Module.php
│   │   ├── controllers
│   │   │   ├── ControllerBase.php
│   │   │   └── IndexController.php
│   │   ├── plugins
│   │   │   └── SecurityPlugin.php
│   │   │   └── NotFoundPlugin.php
│   │   ├── cache
│   │   │   └── cache1.php
│   │   │   └── cache2.php
│   │   └── views
│   │       ├── index
│   │       │   └── index.phtml
│   │       └── index.phtml
│   │
│   └── api
│       └── index.php
│       └── Module.php
│       
├── .htaccess
├── index.html
└── public
    └── index.php
    └── .htaccess
edited Apr '16

As i wrote earlier you can't have controllers in micro application. If you want controllers then use full apllication. Did you even need this api ? Why just not using full aplication and make it rest ? Micro application is just for small applications, fast creating api, adding api to our current application etc. If you gonna build something bigger from scratch then use full application i think.

Actually you can have controllers in micro, it's just not done automatically, see Phalcon\Mvc\Micro\Collection https://docs.phalcon.io/en/latest/reference/micro.html#using-controllers-as-handlers

Oh, didn't know about it :D

Actually I have a single application and my rest api is in RestController and each action is a web service. But I think this not the best way to respect the REST architecture because my REST API is in my Phalcon Web Application. That is why I want to pull out my REST API and build my REST API in a micro application. My REST API and my Web Application use the same models folder and the same database, and I don't want to duplicate my models. And I ask this question in this forum to get some advices about that.

So your normal application just render views etc? Then okay it is good idea to create separate micro/medium application to be just rest API.

edited Apr '16

I don't understand the behavior of the PostController in the medium. How the PostController can communicate with the micro index.php ? And what is this action ?

 public function show($slug)
    {
        // ...
    }

I'm supposed code my web service in this function ?

PostController don't communicate with index.php, it's using di as always and everywhere. First read docs, how phalcon works etc and then ask the questions. All you have in docs: https://docs.phalcon.io/en/latest/reference/micro.html#using-controllers-as-handlers.

Just ask question what you don't understand. This action is just executed when you access:

/posts/show/something

edited Apr '16

I would also recommend reading the docs about collections, but here is how i accomplished the task with multiple collections :

// Create the main collections object $collections = (object) array();

// Create the new collection $collections->RestApiCollection = new MicroCollection();

// Sets the controller (class) for this collection with lazy loading $collections->RestApiCollection->setHandler('RestApiController', true);

// Define a route to match /users with the method GET and to call a function called show_users in the RestApiController class if the route is matched $collections->RestApiCollection->get('/users', 'show_users');

// Go through each collection and mount it to the app foreach ($collections as $collection):

// Mount collection $this->app->mount($collection);

endforeach;