You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that we are able to inject any dependencies we need into the command's constructor. The Laravel [service container](/docs/{{version}}/container) will automatically inject all dependencies type-hinted in the constructor. For greater code reusability, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks.
@@ -101,14 +101,14 @@ Note that we are able to inject any dependencies we need into the command's cons
101
101
}
102
102
103
103
<aname="command-io"></a>
104
-
## Command I/O
104
+
## 命令 I/O
105
105
106
106
<aname="defining-input-expectations"></a>
107
-
### Defining Input Expectations
107
+
### 定义输入期望值
108
108
109
-
When writing console commands, it is common to gather input from the user through arguments or options. Laravel makes it very convenient to define the input you expect from the user using the `signature` property on your commands. The `signature` property allows you to define the name, arguments, and options for the command in a single, expressive, route-like syntax.
All user supplied arguments and options are wrapped in curly braces, for example:
111
+
所有用户输入的参数和选项会被包入大括号中,例如:
112
112
113
113
/**
114
114
* The name and signature of the console command.
@@ -117,15 +117,15 @@ All user supplied arguments and options are wrapped in curly braces, for example
117
117
*/
118
118
protected $signature = 'email:send {user}';
119
119
120
-
In this example, the command defines one **required** argument: `user`. You may also make arguments optional and define default values for optional arguments:
Options, like arguments, also are a form of user input. However, they are prefixed by two hyphens (`--`) when they are specified on the command line. We can define options in the signature like so:
In this example, the `--queue` switch may be specified when calling the Artisan command. If the `--queue` switch is passed, the value of the option will be `true`. Otherwise, the value will be `false`:
You may also specify that the option should be assigned a value by the user by suffixing the option name with a `=` sign, indicating that a value should be provided:
141
+
你也可以让用户给这个选项赋值通过在选项后加等于号`=`,来指示这个值将会由用户来指定:
142
142
143
143
/**
144
144
* The name and signature of the console command.
@@ -147,17 +147,17 @@ You may also specify that the option should be assigned a value by the user by s
In this example, the user may pass a value for the option like so:
150
+
这个例子中,用户可以像这样给这个选项赋值:
151
151
152
152
php artisan email:send 1 --queue=default
153
153
154
-
You may also assign default values to options:
154
+
你也可以给这个选项指定默认值:
155
155
156
156
email:send {user} {--queue=default}
157
157
158
-
#### Input Descriptions
158
+
#### 输入描述
159
159
160
-
You may assign descriptions to input arguments and options by separating the parameter from the description using a colon:
160
+
你可以为参数和选项定义描述如下通过冒号分割参数与描述:
161
161
162
162
/**
163
163
* The name and signature of the console command.
@@ -169,11 +169,11 @@ You may assign descriptions to input arguments and options by separating the par
169
169
{--queue= : Whether the job should be queued}';
170
170
171
171
<aname="retrieving-input"></a>
172
-
### Retrieving Input
172
+
### 获取输入
173
173
174
-
While your command is executing, you will obviously need to access the values for the arguments and options accepted by your command. To do so, you may use the `argument`and`option` methods:
To retrieve the value of an argument, use the `argument` method:
176
+
获取某个参数的值,使用`argument`方法:
177
177
178
178
/**
179
179
* Execute the console command.
@@ -187,24 +187,24 @@ To retrieve the value of an argument, use the `argument` method:
187
187
//
188
188
}
189
189
190
-
If you need to retrieve all of the arguments as an `array`, call the `argument` with no parameters:
190
+
如果你需要获取所有参数的值来作为一个数组,调用无参方法`argument`:
191
191
192
192
$arguments = $this->argument();
193
193
194
-
Options may be retrieved just as easily as arguments using the `option` method. Like the `argument` method, you may call `option` without any arguments in order to retrieve all of the options as an `array`:
If the argument or option does not exist, `null` will be returned.
202
+
如果参数和选项不存在,将返回空值`null`。
203
203
204
204
<aname="prompting-for-input"></a>
205
-
### Prompting For Input
205
+
### 输入提示
206
206
207
-
In addition to displaying output, you may also ask the user to provide input during the execution of your command. The `ask` method will prompt the user with the given question, accept their input, and then return the user's input back to your command:
@@ -216,13 +216,13 @@ In addition to displaying output, you may also ask the user to provide input dur
216
216
$name = $this->ask('What is your name?');
217
217
}
218
218
219
-
The `secret` method is similar to `ask`, but the user's input will not be visible to them as they type in the console. This method is useful for asking for sensitive information such as a password:
$password = $this->secret('What is the password?');
222
222
223
-
#### Asking For Confirmation
223
+
#### 请求确认
224
224
225
-
If you need to ask the user for a simple confirmation, you may use the `confirm` method. By default, this method will return `false`. However, if the user enters `y` in response to the prompt, the method will return `true`.
0 commit comments