-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.html
executable file
·263 lines (186 loc) · 9.97 KB
/
commands.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Documentation</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/style.css">
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
<div class="wrapper">
<header>
<h1>Documentation</h1>
</header>
<aside class="sidebar">
<ul>
<li>Preface
<ul>
<li><a href="index.html">Introduction</a></li>
<li><a href="contributing.html">Contributing</a></li>
</ul></li>
<li>Getting Started
<ul>
<li><a href="installation.html">Installation</a></li>
<li><a href="configuration.html">Configuration</a></li>
<li><a href="lifecycle.html">Request Lifecycle</a></li>
<li><a href="routing.html">Routing</a></li>
<li><a href="requests.html">Requests & Input</a></li>
<li><a href="responses.html">Views & Responses</a></li>
<li><a href="controllers.html">Controllers</a></li>
<li><a href="errors.html">Errors & Logging</a></li>
</ul></li>
<li>Learning More
<ul>
<li><a href="cache.html">Cache</a></li>
<li><a href="events.html">Events</a></li>
<li><a href="html.html">Forms & HTML</a></li>
<li><a href="ioc.html">IoC Container</a></li>
<li><a href="localization.html">Localization</a></li>
<li><a href="mail.html">Mail</a></li>
<li><a href="packages.html">Package Development</a></li>
<li><a href="pagination.html">Pagination</a></li>
<li><a href="queues.html">Queues</a></li>
<li><a href="security.html">Security</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="templates.html">Templates</a></li>
<li><a href="testing.html">Unit Testing</a></li>
<li><a href="validation.html">Validation</a></li>
</ul></li>
<li>Database
<ul>
<li><a href="database.html">Basic Usage</a></li>
<li><a href="queries.html">Query Builder</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">Schema Builder</a></li>
<li><a href="migrations.html">Migrations & Seeding</a></li>
<li><a href="redis.html">Redis</a></li>
</ul></li>
<li>Artisan CLI
<ul>
<li><a href="artisan.html">Overview</a></li>
<li><a href="commands.html">Development</a></li>
</ul></li>
</ul>
</aside>
<section class="content">
<h1>Artisan Development</h1>
<ul>
<li><a href="commands.html#introduction">Introduction</a></li>
<li><a href="commands.html#building-a-command">Building A Command</a></li>
<li><a href="commands.html#registering-commands">Registering Commands</a></li>
<li><a href="commands.html#calling-other-commands">Calling Other Commands</a></li>
</ul>
<p><a name="introduction"></a></p>
<h2>Introduction</h2>
<p>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 <code>app/commands</code> directory; however, you are free to choose your own storage location as long as your commands can be autoloaded based on your <code>composer.json</code> settings.</p>
<p><a name="building-a-command"></a></p>
<h2>Building A Command</h2>
<h3>Generating The Class</h3>
<p>To create a new command, you may use the <code>command:make</code> Artisan command, which will generate a command stub to help you get started:</p>
<p><strong>Generate A New Command Class</strong></p>
<pre><code>php artisan command:make FooCommand
</code></pre>
<p>By default, generated commands will be stored in the <code>app/commands</code> directory; however, you may specify custom path or namespace:</p>
<pre><code>php artisan command:make FooCommand --path="app/classes" --namespace="Classes"
</code></pre>
<h3>Writing The Command</h3>
<p>Once your command is generated, you should fill out the <code>name</code> and <code>description</code> properties of the class, which will be used when displaying your command on the <code>list</code> screen.</p>
<p>The <code>fire</code> method will be called when your command is executed. You may place any command logic in this method.</p>
<h3>Arguments & Options</h3>
<p>The <code>getArguments</code> and <code>getOptions</code> 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.</p>
<p>When defining <code>arguments</code>, the array definition values represent the following:</p>
<pre><code>array($name, $mode, $description, $defaultValue)
</code></pre>
<p>The argument <code>mode</code> may be any of the following: <code>InputArgument::REQUIRED</code> or <code>InputArgument::OPTIONAL</code>.</p>
<p>When defining <code>options</code>, the array definition values represent the following:</p>
<pre><code>array($name, $shortcut, $mode, $description, $defaultValue)
</code></pre>
<p>For options, the argument <code>mode</code> may be: <code>InputOption::VALUE_REQUIRED</code>, <code>InputOption::VALUE_OPTIONAL</code>, <code>InputOption::VALUE_IS_ARRAY</code>, <code>InputOption::VALUE_NONE</code>.</p>
<p>The <code>VALUE_IS_ARRAY</code> mode indicates that the switch may be used multiple times when calling the command:</p>
<pre><code>php artisan foo --option=bar --option=baz
</code></pre>
<p>The <code>VALUE_NONE</code> option indicates that the option is simply used as a "switch":</p>
<pre><code>php artisan foo --option
</code></pre>
<h3>Retrieving Input</h3>
<p>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 <code>argument</code> and <code>option</code> methods:</p>
<p><strong>Retrieving The Value Of A Command Argument</strong></p>
<pre><code>$value = $this->argument('name');
</code></pre>
<p><strong>Retrieving All Arguments</strong></p>
<pre><code>$arguments = $this->argument();
</code></pre>
<p><strong>Retrieving The Value Of A Command Option</strong></p>
<pre><code>$value = $this->option('name');
</code></pre>
<p><strong>Retrieving All Options</strong></p>
<pre><code>$options = $this->option();
</code></pre>
<h3>Writing Output</h3>
<p>To send output to the console, you may use the <code>info</code>, <code>comment</code>, <code>question</code> and <code>error</code> methods. Each of these methods will use the appropriate ANSI colors for their purpose.</p>
<p><strong>Sending Information To The Console</strong></p>
<pre><code>$this->info('Display this on the screen');
</code></pre>
<p><strong>Sending An Error Message To The Console</strong></p>
<pre><code>$this->error('Something went wrong!');
</code></pre>
<h3>Asking Questions</h3>
<p>You may also use the <code>ask</code> and <code>confirm</code> methods to prompt the user for input:</p>
<p><strong>Asking The User For Input</strong></p>
<pre><code>$name = $this->ask('What is your name?');
</code></pre>
<p><strong>Asking The User For Secret Input</strong></p>
<pre><code>$password = $this->secret('What is the password?');
</code></pre>
<p><strong>Asking The User For Confirmation</strong></p>
<pre><code>if ($this->confirm('Do you wish to continue? [yes|no]'))
{
//
}
</code></pre>
<p>You may also specify a default value to the <code>confirm</code> method, which should be <code>true</code> or <code>false</code>:</p>
<pre><code>$this->confirm($question, true);
</code></pre>
<p><a name="registering-commands"></a></p>
<h2>Registering Commands</h2>
<p>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 <code>app/start/artisan.php</code> file. Within this file, you may use the <code>Artisan::add</code> method to register the command:</p>
<p><strong>Registering An Artisan Command</strong></p>
<pre><code>Artisan::add(new CustomCommand);
</code></pre>
<p>If your command is registered in the application <a href="ioc.html">IoC container</a>, you may use the <code>Artisan::resolve</code> method to make it available to Artisan:</p>
<p><strong>Registering A Command That Is In The IoC Container</strong></p>
<pre><code>Artisan::resolve('binding.name');
</code></pre>
<p><a name="calling-other-commands"></a></p>
<h2>Calling Other Commands</h2>
<p>Sometimes you may wish to call other commands from your command. You may do so using the <code>call</code> method:</p>
<p><strong>Calling Another Command</strong></p>
<pre><code>$this->call('command.name', array('argument' => 'foo', '--option' => 'bar'));
</code></pre>
<footer>
<p><a href="http://four.laravel.com" title="Laravel 4 Documentation">Laravel 4 Documentation</a> grabbed by <a href="http://roes-wibowo.com" title="Roes Wibowo">Roes Wibowo</a>.</p>
</footer>
</section>
<div class="clearfix"></div>
</div>
<script src="js/vendor/jquery-1.8.2.min.js"></script>
<script src="js/vendor/prettify.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<script>
var _gaq=[['_setAccount','UA-37104170-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
</body>
</html>