|
| 1 | +# Laravel Docs |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the `list` command: |
| 6 | + |
| 7 | + php artisan list |
| 8 | + |
| 9 | +Every command also includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, precede the name of the command with `help`: |
| 10 | + |
| 11 | + php artisan help migrate |
| 12 | + |
| 13 | +### Tinker (REPL) |
| 14 | + |
| 15 | +Laravel Tinker is a powerful REPL for the Laravel framework, powered by the [PsySH](https://github.com/bobthecow/psysh) package. |
| 16 | + |
| 17 | +#### Installation |
| 18 | + |
| 19 | +All Laravel applications include Tinker by default. However, you may install it manually if needed using Composer: |
| 20 | + |
| 21 | + composer require laravel/tinker |
| 22 | + |
| 23 | +#### Usage |
| 24 | + |
| 25 | +Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the `tinker` Artisan command: |
| 26 | + |
| 27 | + php artisan tinker |
| 28 | + |
| 29 | +You can publish Tinker's configuration file using the `vendor:publish` command: |
| 30 | + |
| 31 | + php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider" |
| 32 | + |
| 33 | +> {note} The `dispatch` helper function and `dispatch` method on the `Dispatchable` class depends on garbage collection to place the job on the queue. Therefore, when using tinker, you should use `Bus::dispatch` or `Queue::push` to dispatch jobs. |
| 34 | +
|
| 35 | +#### Command Whitelist |
| 36 | + |
| 37 | +Tinker utilizes a white-list to determine which Artisan commands are allowed to be run within its shell. By default, you may run the `clear-compiled`, `down`, `env`, `inspire`, `migrate`, `optimize`, and `up` commands. If you would like to white-list more commands you may add them to the `commands` array in your `tinker.php` configuration file: |
| 38 | + |
| 39 | + 'commands' => [ |
| 40 | + // App\Console\Commands\ExampleCommand::class, |
| 41 | + ], |
| 42 | + |
| 43 | +#### Classes That Should Not Be Aliased |
| 44 | + |
| 45 | +Typically, Tinker automatically aliases classes as you require them in Tinker. However, you may wish to never alias some classes. You may accomplish this by listing the classes in the `dont_alias` array of your `tinker.php` configuration file: |
| 46 | + |
| 47 | + 'dont_alias' => [ |
| 48 | + App\User::class, |
| 49 | + ], |
| 50 | + |
| 51 | +## Writing Commands |
| 52 | + |
| 53 | +In addition to the commands provided with Artisan, you may also build your own custom commands. Commands are typically stored in the `app/Console/Commands` directory; however, you are free to choose your own storage location as long as your commands can be loaded by Composer. |
| 54 | + |
| 55 | +<a name="generating-commands"></a> |
| 56 | +### Generating Commands |
| 57 | + |
| 58 | +To create a new command, use the `make:command` Artisan command. This command will create a new command class in the `app/Console/Commands` directory. Don't worry if this directory does not exist in your application, since it will be created the first time you run the `make:command` Artisan command. The generated command will include the default set of properties and methods that are present on all commands: |
| 59 | + |
| 60 | + php artisan make:command SendEmails |
| 61 | + |
| 62 | +<a name="command-structure"></a> |
| 63 | +### Command Structure |
| 64 | + |
| 65 | +After generating your command, you should fill in the `signature` and `description` properties of the class, which will be used when displaying your command on the `list` screen. The `handle` method will be called when your command is executed. You may place your command logic in this method. |
| 66 | + |
| 67 | +> {tip} For greater code reuse, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks. In the example below, note that we inject a service class to do the "heavy lifting" of sending the e-mails. |
| 68 | +
|
| 69 | +Let's take a look at an example command. Note that we are able to inject any dependencies we need into the command's `handle` method. The Laravel [service container](/docs/{{version}}/container) will automatically inject all dependencies that are type-hinted in this method's signature: |
| 70 | + |
| 71 | + <?php |
| 72 | + |
| 73 | + namespace App\Console\Commands; |
| 74 | + |
| 75 | + use App\DripEmailer; |
| 76 | + use App\User; |
| 77 | + use Illuminate\Console\Command; |
| 78 | + |
| 79 | + class SendEmails extends Command |
| 80 | + { |
| 81 | + /** |
| 82 | + * The name and signature of the console command. |
| 83 | + * |
| 84 | + * @var string |
| 85 | + */ |
| 86 | + protected $signature = 'email:send {user}'; |
| 87 | + |
| 88 | + /** |
| 89 | + * The console command description. |
| 90 | + * |
| 91 | + * @var string |
| 92 | + */ |
| 93 | + protected $description = 'Send drip e-mails to a user'; |
| 94 | + |
| 95 | + /** |
| 96 | + * Create a new command instance. |
| 97 | + * |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + public function __construct() |
| 101 | + { |
| 102 | + parent::__construct(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Execute the console command. |
| 107 | + * |
| 108 | + * @param \App\DripEmailer $drip |
| 109 | + * @return mixed |
| 110 | + */ |
| 111 | + public function handle(DripEmailer $drip) |
| 112 | + { |
| 113 | + $drip->send(User::find($this->argument('user'))); |
| 114 | + } |
| 115 | + } |
0 commit comments