Laravel - Artisan Console

Laravel - Artisan Console

1. Introduction to Artisan

Artisan is an essential tool for Laravel development. It provides several useful commands to streamline common tasks such as running servers, caching, migrating databases, and more. Some basic Artisan commands are:

Common Artisan Commands:

  • Starting the Laravel Development Server:

    php artisan serve

    This will start a local development server on http://localhost:8000.

  • Caching the Routes:

    php artisan route:cache

    This command helps speed up your application by caching the routes.

  • Listing All Available Commands:

    php artisan list

    This will display a list of all available Artisan commands that Laravel supports.

  • Getting Help on Specific Commands:

    php artisan help serve

    This will show you detailed help about the serve command, including available options and arguments.

2. Writing Custom Commands

Laravel allows you to create your own commands. This is useful when you need to perform tasks that aren’t already available in Artisan, such as automating processes or handling repetitive tasks.

Steps to Create a Custom Command:

  • Step 1: Create the Command: To create a new command, run the following Artisan command:

    php artisan make:console <name-of-command>

    This will generate a new command file inside the app/Console/Commands directory.

  • Step 2: Default Command Example A new file, such as DefaultCommand.php, will be generated with the following structure:

    <?php namespace App\Console\Commands; use Illuminate\Console\Command; class DefaultCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // Logic to be executed when the command is run } }
  • Step 3: Register the Command The command must be registered in the Kernel.php file so that Artisan recognizes it. In the app/Console/Kernel.php file, you will find an array called $commands. You need to add your custom command here:

    protected $commands = [ Commands\DefaultCommand::class, ];
  • Step 4: Scheduling Commands (Optional) You can schedule your commands to run at specific intervals (e.g., hourly, daily). In the schedule method of Kernel.php, you can specify how frequently the command should run.

    protected function schedule(Schedule $schedule) { // Example: Running a command every hour $schedule->command('command:name')->hourly(); }

3. Viewing Command Details

Once you've defined a custom command, you can view its details using the help command in Artisan. For example, to see the details for your DefaultCommand, run:

php artisan help DefaultCommand

This will display the signature, description, and available options for the command you defined.

4. Example Command Flow

Let's go over a full example, step by step:

  1. Create the Command:

    php artisan make:console SendEmailReminder
  2. Edit the Command File: Open the file app/Console/Commands/SendEmailReminder.php and define the signature and the handle method that contains the logic for sending email reminders.

    protected $signature = 'send:reminder'; public function handle() { // Your email logic here, e.g., sending reminder emails $this->info('Reminder emails sent successfully!'); }
  3. Register the Command: In the app/Console/Kernel.php, add this command to the $commands array.

    protected $commands = [ Commands\SendEmailReminder::class, ];
  4. Schedule the Command (Optional): You can schedule the command to run periodically, such as once a day:

    protected function schedule(Schedule $schedule) { $schedule->command('send:reminder')->daily(); }
  5. Run the Command: To run the command manually, use the following Artisan command:

    php artisan send:reminder
  6. Check Command Details: You can also check the details of your command:

    php artisan help send:reminder

Conclusion

Artisan is a powerful tool in Laravel, allowing you to automate tasks, interact with your application’s components, and streamline your workflow. By creating custom commands, you can enhance the functionality of your Laravel application and ensure tasks are handled efficiently. Additionally, scheduling commands ensure that recurring tasks can be handled automatically without manual intervention.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close