Courses

Laravel API Code Review and Refactor

  • 15 lessons
  • 56 min read

The full course is available only for premium members.

Login Or Join Premium ($129/year or $29/mo)
Can I buy ONLY THIS course without subscription?

About this course

I was asked to review this API project code with just a few Controllers:

I realized this code would be a great candidate for not only giving advice but actually making the changes I suggest.

So I've made exactly that: I spent a few days on refactoring.

And then, I spent a week writing it out in these 15 lessons of this course, with explanations and related links.

From this course, you will learn:

  • Common API Responses: How (NOT) to implement them in Base API Controller or Trait
  • Roles/Permissions: How to use Gates/Policies effectively, with no extra unnecessary code layers
  • API Resources: How to properly use their power, without adding extra response()->json() code
  • Try-catch and Exceptions: How to not overcomplicate Controllers with try-catch blocks, and catch Exceptions globally in the boostrap/app.php, instead
  • Enums: How to use PHP Enums instead of hardcoded values like 'P' or 'F'
  • Tests: How (and WHY) to write automates tests for API
  • And, generally, how to refactor code carefully, step-by-step, with no/minimal breaking changes to the API

The biggest problem with the code was that it was written NOT in "the Laravel way". Many framework features were not utilized properly or fully: Resource Controllers, Route Model Binding, Exception handling, etc.

GitHub repository is included in the course, with the links to specific commits related to a specific lesson.

Comment from one of the students:


So, let's start the review with the first lesson: we will try to run Laravel Pint for styling.