Commands

Dependency Injection

  • Should be done in the handle method

Output

  • Be clear and concise
  • Use the info, comment, question, and error methods
    • Add verbosity levels where necessary
  • Use the newLine method for blank lines
  • Use the table method for tabular data
  • Use the progress method for progress bars

Exit Codes

  • Do not return 0 for success (this is the default)
  • Use 1 for general errors
    • Use the fail method where possible

Examples

class CleanUpUsers extends Command
{
    public function handle()
    {
        $this->info('Starting user cleanup...');
        //...
        foreach ($users as $user) {
            $this->info('Cleaning up user: ' . $user->name, 'v');
            //...
            if (! $user->isInactive()) {
                $this->fail('User is not inactive: ' . $user->name);
            }
        }

        $this->info('{$userCount} users cleaned up.');
    }
}
Previous
Home