Recently I was working on a mobile app project with my friend, Steven where the user can receive location updates from friends and family. Each user will have a tracking code and a list of users with their most recent location.
The biggest challenge of this project was the timeline. We needed to deliver it within two weeks while working a day job.
However, we managed to deliver it in one weekend.
I was assigned to build the backend API for the mobile app. In this article, I’d like to share with you the key practices that I used to deliver the backend API in such a short time.
- Design your API before writing code
- Write API with Lumen, not Laravel
- Simplify your code by using Single Action Controllers
Design your API before writing code
When starting development, most of the inexperienced developers jump into writing code immediately.
That is a common mistake.
Without designing your API before writing code,
- You could be frustrated by the unexpected workload in the later stage
- You could miss the deadline
Draft API endpoints
Here is the example API I drafted.
After you have the first version of the draft ready. You can start the discussion with mobile app developer or client.
Discuss with mobile app developer or client
After the discussion, you have clear requirements and documentation (API draft). Then, you can start to prototype your APIs.
Prototype your API endpoints without writing code
One of my favourite API development tools is Apiary.
Before spending hours on development, you can prototype your API endpoints on Apiary. Apiary provides a mock server for your API which means your API is instantly ready to be called and response as documented.
Mobile app development can start immediately without having to wait for the actual backend API to be ready.
The mobile app should send the request to the API as documented, and backend API should return the response as designed.
Write API with Lumen, not Laravel
Lumen is the lighter version of Laravel which is specifically designed for API development.
The first reason I recommend Lumen is easier to learn. Lumen is specifically designed for stateless API, so you don’t need to learn Session, Views and some other 3rd party components.
The second reason is the speed.
Check out this performance comparison done by Laurence (Creator of Eyewitness.io — A Laravel monitoring application)
Without touching the database, Lumen is 22.82% faster than Laravel (web) and 13.89% faster than Laravel (api).
By selecting data from the database, Lumen is 16.67% faster than Laravel (web) and 6.13% faster than Laravel (api).
By inserting and deleting from the database, Lumen is 33.08% faster than Laravel (web) and 16.02% faster than Laravel (api).
Generally, Lumen is 24.19% faster than Laravel (web) and 12.01% faster than Laravel (api).
Compatible with Laravel
The third reason is you can shift to Laravel easily as application codes are compatible with both frameworks.
When the application grows, you can shift to Laravel anytime to serve the application’s needs.
Simplify your code by using Single Action Controllers
To keep your code simple and easy to maintain, implement Single Action Controller from Laravel.
Single Action Controller is one controller performs one action only. For example, add a tracking code or get a tracking list.
Here are my single action controllers look like in one of my projects.
And the other single action controllers in the project.
And, the routes.
What about the shared code? If you have a function which is needed by more than one controllers, you can write a Trait.
Tech stack I was using in this project.
Have you try Apiary, have you not? This is the tutorial that can help you get started. If you did, what’s your experience using Apiary?
Have you ever use Lumen in real life project before? Email me about your experience at email@example.com
This post is also posted at TribeHired. You can read it at https://tribehired.com/blog/ultimate-guide-to-build-a-mobile-backend-api-in-short-time-for-laravel-developers/tribehired.