TOS PHP API skeleton for mobile apps using ORM

It was not a usual hectic day at TOS. My clients were so silent that I thought I could manage some time to improve our custom API module for mobile apps. I was about to use Laravel for that, but thought that would be an overkill and decided not to.

So, the modification was limited in replacing two of the core php classes which we had been using for quite a long time.

I used to do the validation part with custom logic. And for data filtering the sanitize class from CakePHP. Now, for both validation and filtering, I chose Gump. Gump is a clean and easy-to-use php class for filtering and validating data. It has a very good documentation on it’s Github page that provides a number of examples.

Another change was incorporating an Object Relational Mapper (ORM) for database connection and query building—Idiorom. I like RedbeanPHP pretty much, but preferred Idiom because the functions for slightly complex queries read pretty neat here. On top of PDO drivers, it works faster and smoother.

Although, ORM increases the execution time, it gives you mental peace if you are the kind of guy who really hates to see the raw sql statements smiling at him from the places they should not be present.

Then, it took a while to modify the existing classes to make them compatible with the newly incorporated ORM. Wrote two new functions for Database helper class – one for changing the character set and collation of the database tables, another one for parsing .sql files and creating tables from frontend by using PDO transaction and commmit.

Finally the skeleton’s folder structure became this:

tos-api-skeleton

I am happy because, from now on, by using this new skeleton, writing APIs for our next mobile apps could be done more conveniently.

Leave a Reply

Your email address will not be published. Required fields are marked *