Adding an artisan command route:check for duplicate routes Adding an artisan command route:check for duplicate routes

January 23, 2022

bash laravel

Today I tried to create a new subcommand for the php artisan route:list command by adding a --duplicates option.

I edited the command in /vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php adding an option and a sub function into getRoutes for duplicates. But it seemed to only have a single entry for each route. The problem is the getRoutes function removes all duplicates, so my array function only counted 1 for each.

So i gave up and wrote this awk command line script to find duplicates in the web.php routes file:

awk -F"'" '$1 ~ /^Route::/ {print $2}' routes/web.php | awk 'NF > 0' | sort | uniq -c -d 

It finds all lines starting with "Route::" and prints the second awk column $2 which is then sorted and the -d on uniq show duplicates.

it outputs a list of routes with a count higher than 1, like so.

2 contact
2 az

I am sure it could be improved, but it solved the problem for me and helped me find the duplicates in a sinlge line.


If you would like to contact me you can either use this form on londinium.com or via Twitter @andylondon