To register Artisan commands outside the default folder in Laravel, you need to follow these steps:
-
Move your command file to the desired folder. In this case, it should be moved to the
/modules/ServiceNow/Consolefolder. -
Update the namespace of your command file to reflect the new folder structure. For example, if your command file is named
MyCommand.phpand it was originally in theApp\Console\Commandsnamespace, you should update it toModules\ServiceNow\Console\Commands. -
Open the
app\Console\Kernel.phpfile and update thecommands()method to load the commands from the new folder. Replace the existing code with the following:
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands'); // Load commands from the default folder
$this->load(__DIR__.'/../../modules/ServiceNow/Console/Commands'); // Load commands from the new folder
require base_path('routes/console.php');
}
Make sure to update the path to the new folder accordingly.
- Save the changes and run the
php artisancommand to see if your Artisan command is now visible. You should be able to see your command listed among the available commands.
By following these steps, you should be able to register Artisan commands outside the default folder in Laravel.