Laravel: Automatically Add Your Console Commands To Kernel
When your application grows and if you often use console commands, you can find out that your ‘app/Console/Kernel.php’ file may look
something like this:
The definition of the $commands property grows very quickly and looks very ugly. How to solve this problem?
All of our commands are usually located in one folder. Of course they can me placed in different subfolders, but the main folder is always one.
So, we can dynamically scan it and fill the $commands property. Let’s override a constructor of the Kernel class. It accepts two arguments:
the application instance and the event dispatcher.
In the code above we simply proxy the passed params to the parent constructor, but we call a new loadCommands method. Here is the code of it:
It recursively scans the passed directory as an argument and appends all founded files to the $commands property.