Command Line Route Helpers in Laravel - Use cases

Consistency shouldn't be just heard, but brought into action.

We always initiate something and get that dopamine rush, and on next iteration we expect a bit more. Since desires aren't restricted by boundaries and when expectation doesn't fulfil, we roam like wild boar without channelising our energy (Philosophy mode : activated). Coming back from Nirvana and jumping straight into our terminal .

This article is continuation from my previous article, so before jumping ahead it is highly recommended to read the first article.

triple-m.hashnode.dev/command-line-route-he..

So now you have firm grip on articulating the route list as per your needs, and you want to dig more into it, and see what can be the possible use cases for it.

Motivation behind digging deep into Route List

We all are aware of Json format and it's benefit in exchanging data. New utilities often provides flexibility to have output in Json format. Even, Route list has in built option to convert the response into Json instead of TABULAR format. This gives ease of use to pass the data which can be further drill down as per requirement.

On a fine day, my manager asked me to jot down all the routes that we have in our website and make CSV out of it. Looking at his forehead deep lines, I was convinced that he needs to have them immediately. So without further ado, I ran into our web.php and api.php to figure out what do we have. Observing the number of lines was making me feel proud that we have built so much in short span, but that quickly faded since now i have to make list of all this in CSV.

After delivering the required report, I was not convinced for the time it took me to do the monotonous work. Hence, I decided to change the gear from manual to automatic and have my cup of tea in peace :p

Plan of Action

I found that route:list output can be modified into Json as follows :

php artisan route:list --columns=method,name --json

hashnode-article-2-img-1.png

After searching around a bit, i found one command line utility which converts Json into Csv, and Voila! we are close to our target now. The utility name is jq . You can install it with following guide

stedolan.github.io/jq/download

Assuming you have jq in place, let's get the desired result. jq provides bunch of options and we will use -r format for our need.

Step by Step Guide to use jq

Step 1 : Use raw Format

We will use the -r format to Output raw string and not Json texts, which will help us to read it much better

php artisan route:list --columns=method,name --json | jq -r

hashnode-article-2-img-2.png

Step 2 : Collect all Headers

In above image we can clearly see what we have in our Json output. Now, we will collect all the headers from individual nested keys.

php artisan route:list --columns=method,name --json | jq -r '(map(keys) | add | unique) as $cols | $cols'

hashnode-article-2-img-3.png

The map method iterates over the Json, add method adds it to the list, unique method filters out the duplication, and the keys are appended in $cols variable.

Step 3 : Collect all Rows

We now have columns in place, let's accumulate the respective values from Json and treat them as individual row in CSV.

php artisan route:list --columns=method,name --json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $rows'

hashnode-article-2-img-4.png

With the previous $cols list in place, it again iterates through Json, and look after the matching values and collect them in $row , now this $row is pushed into the main array of $rows , and thus we have Array of Array in place.

Step 4 : Merging all and piping into CSV

The building blocks of collecting Headers and Values are in place, we will club them and get the expected CSV output.

php artisan route:list --columns=method,name,uri --json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' > routeList.csv

hashnode-article-2-img-5.png

Conclusion

You have learnt how to quickly get all the routes in your application in Json and Csv format. It can be further modified by appending hostname to that routes. I will leave upto you to do that.

Thank you for reading it till the end. You have made it :)

If you have reached till this point, I am sure you will like my twitter feed too [twitter.com/ManghwaniManish]

P.s.: I have tendency to give credits to respectable person for their efforts. I have referred following site for writing this article.

freecodecamp.org/news/how-to-transform-json..