## 創建自定義指令
第一步,創建一個自定義命令類文件,運行指令
~~~
php think make:command Hello hello
~~~
會生成一個`app\command\Hello`命令行指令類,我們修改內容如下:
~~~
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Hello extends Command
{
? ? protected function configure()
? ? {
? ? ? ? $this->setName('hello')
->addArgument('name', Argument::OPTIONAL, "your name")
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('Say Hello');
? ? }
? ? protected function execute(Input $input, Output $output)
? ? {
$name = trim($input->getArgument('name'));
$name = $name ?: 'thinkphp';
if ($input->hasOption('city')) {
$city = PHP_EOL . 'From ' . $input->getOption('city');
} else {
$city = '';
}
? ? ? ? $output->writeln("Hello," . $name . '!' . $city);
? ? }
}
~~~
這個文件定義了一個叫`hello`的命令,并設置了一個`name`參數和一個`city`選項。
第二步,配置`config/console.php`文件
~~~
<?php
return [
'commands' => [
? ? 'hello' => 'app\command\Hello',
]
];
~~~
第三步,測試-命令幫助-命令行下運行
~~~
php think
~~~
輸出
~~~cmd
Think Console version 0.1
Usage:
? command [options] [arguments]
Options:
? -h, --help ? ? ? ? ? ?Display this help message
? -V, --version ? ? ? ? Display this console version
? -q, --quiet ? ? ? ? ? Do not output any message
? ? ? --ansi ? ? ? ? ? ?Force ANSI output
? ? ? --no-ansi ? ? ? ? Disable ANSI output
? -n, --no-interaction ?Do not ask any interactive question
? -v|vv|vvv, --verbose ?Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
? build ? ? ? ? ? ? ?Build Application Dirs
? clear ? ? ? ? ? ? ?Clear runtime file
hello? ? ? ? ? ? ? Say Hello
? help ? ? ? ? ? ? ? Displays help for a command
? list ? ? ? ? ? ? ? Lists commands
?make
? make:controller ? ?Create a new resource controller class
? make:model ? ? ? ? Create a new model class
?optimize
? optimize:autoload ?Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
? optimize:config ? ?Build config and common file cache.
? optimize:schema ? ?Build database schema cache.
~~~
第四步,運行`hello`命令
~~~cmd
php think hello
~~~
輸出
~~~
Hello thinkphp!
~~~
添加命令參數
~~~cmd
php think hello kancloud
~~~
輸出
~~~
Hello kancloud!
~~~
添加`city`選項
~~~cmd
php think hello kancloud --city shanghai
~~~
輸出
~~~
Hello kancloud!
From shanghai
~~~
>[danger] 注意看參數和選項的調用區別
>
如果需要生成一個指定的命名空間,可以使用:
~~~
php think make:command app\index\Command second
~~~
## 在控制器中調用命令
支持在控制器的操作方法中直接調用命令,例如:
~~~
<?php
namespace app\index\controller;
use think\facade\Console;
class Index
{
public function hello($name)
{
$output = Console::call('hello', [$name]);
return $output->fetch();
}
}
~~~
訪問該操作方法后,例如:
~~~
http://serverName/index/hello/name/thinkphp
~~~
頁面會輸出
~~~
Hello thinkphp!
~~~
## 命令行選項
```
use think\\console\\input\\Option;
// 無需傳值
Option::VALUE_NONE ? ? = 1;
// 必須傳值
Option::VALUE_REQUIRED = 2;
// 可選傳值
Option::VALUE_OPTIONAL = 4;
// 傳數組值
Option::VALUE_IS_ARRAY = 8;
```
## addOption
```
/**
* 添加選項
* @param string $name ? ? ? ?選項名稱
* @param string $shortcut ? ?別名
* @param int $mode ? ? ? ?類型
* @param string $description 描述
* @param mixed $default ? ? 默認值
* @return Command
*/
public function addOption(string $name, string $shortcut = null, int $mode = null, string $description = '', $default = null)
```
### 示例
定義如下:
```
->addOption('adminname', 'p', Option::VALUE_OPTIONAL, '管理員賬號')
```
調用方式可以如下:
```
// 無需任何參數
php think install
// 使用全名參數
php think install --adminuser admin
// 使用縮寫
php think install -u admin
```
指令邏輯中可以這樣獲取:
```
$input->getOption('adminname');
```
- 序言
- 基礎
- 安裝
- 開發規范
- 目錄結構
- 配置
- 架構
- 請求流程
- 架構總覽
- 入口文件
- 多應用模式
- URL訪問
- 容器和依賴注入
- 服務
- 門面
- 中間件
- 事件
- 路由
- 路由定義
- 變量規則
- 路由地址
- 路由參數
- 路由中間件
- 路由分組
- 資源路由
- 注解路由
- 路由綁定
- 域名路由
- MISS路由
- 跨域請求
- URL生成
- 控制器
- 控制器定義
- 基礎控制器
- 空控制器
- 資源控制器
- 控制器中間件
- 請求
- 請求對象
- 請求信息
- 輸入變量
- 請求類型
- HTTP頭信息
- 偽靜態
- 參數綁定
- 請求緩存
- 響應
- 響應輸出
- 響應參數
- 重定向
- 文件下載
- 數據庫
- 連接數據庫
- 分布式數據庫
- 查詢構造器
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 查詢表達式
- 鏈式操作
- where
- table
- alias
- field
- strict
- limit
- page
- order
- group
- having
- join
- union
- distinct
- lock
- cache
- cacheAlways
- comment
- fetchSql
- force
- partition
- failException
- sequence
- replace
- extra
- duplicate
- procedure
- 聚合查詢
- 分頁查詢
- 時間查詢
- 高級查詢
- 視圖查詢
- JSON字段
- 子查詢
- 原生查詢
- 獲取查詢參數
- 查詢事件
- 獲取器
- 事務操作
- 存儲過程
- 數據集
- 數據庫驅動
- 模型
- 定義
- 模型字段
- 新增
- 更新
- 刪除
- 查詢
- 查詢范圍
- JSON字段
- 獲取器
- 修改器
- 搜索器
- 數據集
- 自動時間戳
- 只讀字段
- 軟刪除
- 類型轉換
- 模型輸出
- 模型事件
- 模型關聯
- 一對一關聯
- 一對多關聯
- 遠程一對多
- 遠程一對一
- 多對多關聯
- 多態關聯
- 關聯預載入
- 關聯統計
- 關聯輸出
- 虛擬模型
- 視圖
- 模板變量
- 視圖過濾
- 模板渲染
- 模板引擎
- 視圖驅動
- 錯誤和日志
- 異常處理
- 日志處理
- 調試
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 遠程調試
- 驗證
- 驗證器
- 驗證規則
- 錯誤信息
- 驗證場景
- 路由驗證
- 內置規則
- 表單令牌
- 注解驗證
- 雜項
- 緩存
- Session
- Cookie
- 多語言
- 上傳
- 命令行
- 啟動內置服務器
- 查看版本
- 自動生成應用目錄
- 創建類庫文件
- 清除緩存文件
- 生成數據表字段緩存
- 生成路由映射緩存
- 輸出路由定義
- 自定義指令
- Debug輸出級別
- 擴展庫
- 數據庫遷移工具
- Workerman
- think助手工具庫
- 驗證碼
- Swoole
- 附錄
- 助手函數
- 升級指導
- 更新日志