Courses

Laravel API Code Review and Refactor

  • 15 lessons
  • 56 min read

About this course

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

Route::apiResource('orders', OrderController::class)->except( ['update'] );
Route::patch('orders/{order}', [ OrderController::class, 'update' ]);
Route::put('orders/{order}', [ OrderController::class, 'replace' ]);
 
Route::apiResource('owners', OrderOwnerController::class);
Route::apiResource('owners.orders', OwnerOrdersController::class);

While looking at it, I realized it 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 spent a week writing it out in these 15 lessons.

By the end of this course, after a lot of "baby steps", the routes will be shortened to this:

Route::apiResource('orders', OrderController::class);
Route::apiResource('users', UserController::class);

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.

Repository included, with the links to specific commits related to a specific lesson. But I've written those 10,000+ words as a course to explain WHY those changes were made, with the links to Laravel docs and additional resources.

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