|
| 1 | +# Artisan Development |
| 2 | + |
| 3 | +- [Introduction](#introduction) |
| 4 | +- [Building A Command](#building-a-command) |
| 5 | +- [Registering Commands](#registering-commands) |
| 6 | +- [Calling Other Commands](#calling-other-commands) |
| 7 | + |
| 8 | +<a name="introduction"></a> |
| 9 | +## Introduction |
| 10 | + |
| 11 | +In addition to the commands provided with Artisan, you may also build your own custom commands for working with your application. You may store your custom commands in the `app/commands` directory; however, you are free to choose your own storage location as long as your commands can be autoloaded based on your `composer.json` settings. |
| 12 | + |
| 13 | +<a name="building-a-command"></a> |
| 14 | +## Building A Command |
| 15 | + |
| 16 | +### Generating The Class |
| 17 | + |
| 18 | +To create a new command, you may use the `command:make` Artisan command, which will generate a command stub to help you get started: |
| 19 | + |
| 20 | +**Generate A New Command Class** |
| 21 | + |
| 22 | + php artisan command:make FooCommand |
| 23 | + |
| 24 | +By default, generated commands will be stored in the `app/commands` directory; however, you may specify custom path or namespace: |
| 25 | + |
| 26 | + php artisan command:make FooCommand --path="app/classes" --namespace="Classes" |
| 27 | + |
| 28 | +### Writing The Command |
| 29 | + |
| 30 | +Once your command is generated, you should fill out the `name` and `description` properties of the class, which will be used when displaying your command on the `list` screen. |
| 31 | + |
| 32 | +The `fire` method will be called when your command is executed. You may place any command logic in this method. |
| 33 | + |
| 34 | +### Arguments & Options |
| 35 | + |
| 36 | +The `getArguments` and `getOptions` methods are where you may define any arguments or options your command receives. Both of these methods return an array of commands, which are described by a list of array options. |
| 37 | + |
| 38 | +When defining `arguments`, the array definition values represent the following: |
| 39 | + |
| 40 | + array($name, $mode, $description, $defaultValue) |
| 41 | + |
| 42 | +The argument `mode` may be any of the following: `InputArgument::REQUIRED` or `InputArgument::OPTIONAL`. |
| 43 | + |
| 44 | +When defining `options`, the array definition values represent the following: |
| 45 | + |
| 46 | + array($name, $shortcut, $mode, $description, $defaultValue) |
| 47 | + |
| 48 | +For options, the argument `mode` may be: `InputOption::VALUE_REQUIRED`, `InputOption::VALUE_OPTIONAL`, `InputOption::VALUE_IS_ARRAY`, `InputOption::VALUE_NONE`. |
| 49 | + |
| 50 | +The `VALUE_IS_ARRAY` mode indicates that the switch may be used multiple times when calling the command: |
| 51 | + |
| 52 | + php artisan foo --option=bar --option=baz |
| 53 | + |
| 54 | +The `VALUE_NONE` option indicates that the option is simply used as a "switch": |
| 55 | + |
| 56 | + php artisan foo --option |
| 57 | + |
| 58 | +### Retrieving Input |
| 59 | + |
| 60 | +While your command is executing, you will obviously need to access the values for the arguments and options accepted by your application. To do so, you may use the `argument` and `option` methods: |
| 61 | + |
| 62 | +**Retrieving The Value Of A Command Argument** |
| 63 | + |
| 64 | + $value = $this->argument('name'); |
| 65 | + |
| 66 | +**Retrieving All Arguments** |
| 67 | + |
| 68 | + $arguments = $this->argument(); |
| 69 | + |
| 70 | +**Retrieving The Value Of A Command Option** |
| 71 | + |
| 72 | + $value = $this->option('name'); |
| 73 | + |
| 74 | +**Retrieving All Options** |
| 75 | + |
| 76 | + $options = $this->option(); |
| 77 | + |
| 78 | +### Writing Output |
| 79 | + |
| 80 | +To send output to the console, you may use the `info`, `comment`, `question` and `error` methods. Each of these methods will use the appropriate ANSI colors for their purpose. |
| 81 | + |
| 82 | +**Sending Information To The Console** |
| 83 | + |
| 84 | + $this->info('Display this on the screen'); |
| 85 | + |
| 86 | +**Sending An Error Message To The Console** |
| 87 | + |
| 88 | + $this->error('Something went wrong!'); |
| 89 | + |
| 90 | +### Asking Questions |
| 91 | + |
| 92 | +You may also use the `ask` and `confirm` methods to prompt the user for input: |
| 93 | + |
| 94 | +**Asking The User For Input** |
| 95 | + |
| 96 | + $name = $this->ask('What is your name?'); |
| 97 | + |
| 98 | +**Asking The User For Confirmation** |
| 99 | + |
| 100 | + if ($this->confirm('Do you wish to continue? [yes|no]')) |
| 101 | + { |
| 102 | + // |
| 103 | + } |
| 104 | + |
| 105 | +You may also specify a default value to the `confirm` method, which should be `true` or `false`: |
| 106 | + |
| 107 | + $this->confirm($question, true); |
| 108 | + |
| 109 | +<a name="registering-commands"></a> |
| 110 | +## Registering Commands |
| 111 | + |
| 112 | +Once your command is finished, you need to register it with Artisan so it will be available for use. This is typically done in the `app/start/artisan.php` file. Within this file, you may use the `Artisan::add` method to register the command: |
| 113 | + |
| 114 | +**Registering An Artisan Command** |
| 115 | + |
| 116 | + Artisan::add(new CustomCommand); |
| 117 | + |
| 118 | +If your command is registered in the application [IoC container](/docs/ioc), you may use the `Artisan::resolve` method to make it available to Artisan: |
| 119 | + |
| 120 | +**Registering A Command That Is In The IoC Container** |
| 121 | + |
| 122 | + Artisan::resolve('binding.name'); |
| 123 | + |
| 124 | +<a name="calling-other-commands"></a> |
| 125 | +## Calling Other Commands |
| 126 | + |
| 127 | +Sometimes you may wish to call other commands from your command. You may do so using the `call` method: |
| 128 | + |
| 129 | +**Calling Another Command** |
| 130 | + |
| 131 | + $this->call('command.name', array('argument' => 'foo', '--option' => 'bar')); |
0 commit comments