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 PHP REST API structure

I am developing REST API using Phalcon. The application will have multiple modules like Users, Company, Contacts and so on. Each of these modules have their own tables for storing data. Moreover each module have their own defination file which mentions what fields to show in API response. I am pretty new to Phalcon, i just started learning, and I need help regarding how i should structure the application so that this code won't give me future problems, or if I'm missing something or if the code could be abstracted more then that would be great.

Directory Structure as planned:

app/

MyAPI/
    MyAPIControler.php

library/

controller.php //master controller where all controllers inherit from
model.php //master model where all models inherit from
utilities.php
MyAPI/

    models/

        User.php 
        Contacts.php
        Company.php
        Myapi.php

/config

config.php
routes.php

index.php

I want all database related queries of each module to reside in their own models. API URL say for listing of records will be https://api.example.com/MyAPI/V2/contacts/list or /MyAPI/V2/users/list. Similarly API URL for creating records will be https://api.example.com/MyAPI/V2/contacts/add or /MyAPI/V2/users/list

Please advise how i should proceed.



79.0k
Accepted
answer
edited May '17

1st to clarify - why you wish for independent modules per API feature while trying to use Phalcon Micro?

IMO that's not desired pattern for a number of reasons. You can do all of that in a single Micro app structure, with Models, Controllers etc.

Or perhaps you are aiming at Microservices architecture?

The thing with Phalcon (Micro) is to use services container (IoC) wisely and DRY. You control your routes with MicroCollection component. And you use simple app events - before, after and finish.



43.9k

HI,

also, a REST app has some specific rules based on http verbs (GET, POST, PUT, DELETE), so



5.8k

Thanks both of you for quick reply. I was under the weather so couldn't replied earlier.

I have changed the directory structure. it goes like this:

app

/config
    config.php
    routes.php
    loader.php
    di.php

/controllers
    mycontroller.php
    Abstractexception.php
    Abstratcontroller.php  #this extends \Phalcon\DI\Injectable

/models
    mymodel.php

/services
    AbstractService.php #this extends \Phalcon\DI\Injectable
    myservice.php 

public

index.php

I can controll the routes using routes.php.

I have many DB tables to handle like Contacts, Users, Accounts etc. I am dynamically setting source for my single model from Service container depending on the request. Is this right thing to do or do you have any suggestions.

Also I have several functions which will be used by various API. Is it advisable to create a library where to keep all the common functions and use it when needed.

Your suggestions is very much appreciated. Thank again.