Skip to content

Find Route Definitions By API Paths In Seconds

Find Route Definitions By API Paths In Seconds

Let’s imagine the following scenario.

You’re working on a monolith or a macroservice project.

And a mobile app developer comes to you and says:

– Hey, can you help me debug this API endpoint?

And they give you the URI of the API (i.e https://example.com/api/v1/device-management/managed-devices/{id}/scripts{id}).

You accept the task like the good colleague that you are and open the IDE and get to work.

Because the project is massive, there is a plethora of routes that are most likely even split into several files.

Imagine finding a route definition just by knowing the API URI or the path.

Some APIs have similar paths, like login, logout, status, settings, password reset for different types of users and use cases, and so on…

It’s almost like you’re trying to find the needle in a haystack.

Let’s now get to the bottom of this.

 

Find routes by artisan command [Laravel only]

If you’re using Laravel, there is a quick way of finding the route definition by using

php artisan route:list --name=<route-name>

I’m going to search for the ‘login’ path and it returned the HTTP verb and the entry point which in my case is the create() method from the AuthenticatedSesionController.

 

Find route definition by API paths in seconds

But I’d like to propose a better way.

Hang on to your hats because this is going to blow your mind!

Simply add a comment showing the API URI or the path above a specific route.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\v1\UserController;
use App\Http\Controllers\v1\MessageController;
/*
* API Routes
*/
Route::group(['prefix' => 'app/users'], function () {
// /api/v1/app/users
Route::get('/', [UserController::class, 'list']);
// /api/v1/app/users/register
Route::post('/register', [UserController::class, 'create']);
});
Route::group(['prefix' => 'app/messages'], function () {
// /api/v1/app/messages/users/{username}/inbox
Route::get('/users/{username}/inbox', [MessageController::class, 'getAllReceived']);
// /api/v1/app/messages/users{username}/sent
Route::get('/users/{username}/sent', [MessageController::class, 'getAllSent']);
// /api/v1/app/messages/users/{username}
Route::post('/users/{username}', [MessageController::class, 'send']);
});
view raw

api.php

hosted with ❤ by GitHub

And next time you want to search for the route definition by API path, you’ll find it in just a few seconds.


Find Route Definitions By API Paths In Seconds
This is an oversimplification of some API endpoints.

I applied this technique in a project at work and the job of finding the routes turned from a hassle into a breeze.

Try it out, and see if it works for you and your team.

At least for us it improved the developer experience and working with the code in general.

Happy coding 😊


Let me know what you think about this article in the comments section below.

If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.   
  

Comments