Commands

Table des matières

1. Introduction

Laravel works with artisan which can allow to automate things. On the command line, we can run commands with php artisan cache:clear. “cache:clear” is a command and we can create our owns.

2. List existing ones

php artisan list

On the command line, return the list of existing commands.

3. Create new command

To add a new command, we can fire php artisan make:command followed by a verb.

For instance :

php artisan make:command clear

This will create a new file in /app/Console/Commands/todos.php.

Two properties are mandatory:

The signature property is the one that we’ll use in php artisan.

The description property is the associated help text.

4. Some actions

4.1. call

$this->call('cache:clear');

Call and execute another command.

4.2. Output

4.2.1. ask

$database = $this->ask('Database name')

Ask the user for a value.

4.2.2. confirm

if ($this->confirm('Do you want to ...?', false)) {
    // The user has answer yes
}

Ask user for confirmation (Yes/No)

4.2.3. info

$this->info('>> Welcome to our installation process! <<');

This will allow to echo on the command prompt an information message (with green background).

5. Example

5.1. concil:install

https://raw.githubusercontent.com/imanghafoori1/council/master/app/Console/Commands/InstallCommand.php

This script allows (in theory) to create a new database upon the installation of the laravel site.