Command line Route Helpers in Laravel

Command line Route Helpers in Laravel

Nutshell is not just a word but an emotion.

We always desire to have precise and quick information. It helps to create a mind map in our mind palace ( sherlock mode : activated ). Coming back from 221B Baker Street and jumping straight to our command line.

We all are aware of following command.

php artisan route:list

image1.png

Initially the project has few routes and controllers, hence it is viable to run the above command, but in real world we usually end up with more than 15-20 controllers and assuming 3-5 routes per Controller which yields around 45-100 route list.

Grab your seats because you are about to see something gigantic.

image2.png

I am not sure if that list will be covered in next iteration. Anyway, coming back to my point. Here comes Route helper in rescue .

There are two ways to reduce the route table :

  1. By Column
  2. By Row

Let's First talk about By Column

There are about 6 column names in route table...wait don't scroll up, here is the list :p

['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware']

So now you have control over column names by using:

php artisan route:list --columns=method

Damn, I can see excitement on your face now. In meantime, I am sure you will be more curious to know if it accepts multiple values for columns.

image3.png

The Answer is YES

You can pass comma separated value to column option and get the desired result.

image4.png

Jumping to 2nd option : By Rows

You might be wondering how can rows be limited since they are dynamic in nature. The answer is all routes are first collected and then operations such as filters are performed.

Suppose you are interested only in GET parameter and want to quickly find How many GET routes are there in my website?
It can be for ample amount of reasons, one of which can be to have a lookup if all are responding with developer friendly 200 status :) . It can be achieved as follows:

php artisan route:list --method=GET

image5.png

Now you have rough idea on how By Column & By Row works. You can perform criss-cross selection to scale it down as per your use case. It was really helpful for me in terms of figuring out for which controller or route i have to write test case. I could quickly narrow down the results with respect to controller and its method.

php artisan route:list --name=analysis --method=get

image6.png

Bonus tip : You can sort by Column and decide sort order too

  • For Ascending order w.r.t Names
php artisan route:list --method=GET --sort=name
  • For Descending order w.r.t Names
php artisan route:list --method=GET --sort=name -r

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: Also, if there is good response on this, I am going to write another article which will have many more exciting helpers. *