Tips on Building a RESTful Laravel API Backend

After some two weeks of casual development, I have nearly completed a Laravel 5 backend for a time tracker app (save for some authentication and role based privileges). Laravel is the first open source PHP framework that I have been exposed to, and so far, it rocks! It’s super clean, super minimal, and still quite flexible.

I opted to implement my Laravel controllers as resource elements. This means that you stick to a certain pattern of routes and controller functions that enable RESTful API implementations. This works great, and there’s been enough resources online to get things done relatively quickly. After some last polishing tomorrow, I hope to start my AngularJS frontend soon.

Often when I want to learn new things, I am at a loss of where to start. In case you are thinking about starting your own Laravel app, here’s a rough breakdown of how to start.

  1. Install Laravel using Composer.
  2. Create a MySQL database to create your tables in.
  3. Name your app.
  4. Update your Laravel environment to point to the created DB. (located in Project/.env)
  5. Create Laravel migrations to create tables.
  6. Create Laravel models to represent your relational data.
  7. Add seed data to populate your database with some dummy data.
  8. Create controllers.
  9. Add routes mapping to controllers.
  10. Refactor.
  11. Unit Test.

There you go!

Some great resources to teach/aid:

  • Eloquent ORM & Models – A very good starting point to get your migrations, seed data and models up and running.
  • Postman – As mentioned in a previous post, to test your end points, especially when you start posting data and working with HTTP verbs like PUT and DELETE.
  • Laracasts series: Incremental API Development – Extremely helpful, not just on good API practices but also on refactoring and keeping code clean. This series also answered some questions I had in previous posts, like error handling.
  • Build API’s You Won’t Hate – A great book on API development. I’d really recommend it if you want to build the API the right way from the start.