[TOC]
## **Command**
### **屬性**
| 屬性名 | 描述 |參數類型|默認值|權限|
| --- | --- | --- | --- | --- |
| $input | | Input | void | protected |
| $output | | Output | void | protected |
### **方法**
| 方法名 | 描述 |參數說明|返回值|權限|
| --- | --- | --- | --- | --- |
| __construct($name = null) | 構造方法 | string\|null $name 命令名稱,如果沒有設置則比如在 configure() 里設置 |void| public |
| ignoreValidationErrors() | 忽略驗證錯誤 |void| void | public |
| setConsole(Console $console = null) | 設置控制臺 |Console| void | public |
| getConsole() | 獲取控制臺 | void | Console |public|
| isEnabled() | 是否有效 | void |直接返回true| public |
| **configure**() | 配置指令,留給子類進行自定義命令參數配置 | void | void | protected |
| **execute**(Input $input, Output $output) | 執行指令,留給子類進行自定義命令執行程序的接口 | Input,Output | null\|int | protected |
| run(Input $input, Output $output) | 執行 | Input,Output | int | public |
| setCode(callable $code) | 設置執行代碼 | callable(InputInterface $input, OutputInterface $output) | Command | public |
| mergeConsoleDefinition($mergeArgs = true) | 合并參數定義 | 合并的參數 | void | public |
| setDefinition($definition) | 設置參數定義 | array\|Definition $definition | Command |public |
| getDefinition() | 獲取參數定義 | void | Definition | public |
| getNativeDefinition() | 獲取當前指令的參數定義 | void | Definition | public |
| **addArgument**($name, $mode = null, $description = '', $default = null) | 添加參數 | string $name 名稱<br>int $mode 類型<br>string $description 描述<br>mixed $default 默認值 | Command | public |
| **addOption**($name, $shortcut = null, $mode = null, $description = '', $default = null) | 添加選項 添加后 使用時跟在參數后面,前面加-- | string $name 選項名稱<br>string $shortcut 別名<br>int $mode 類型<br>string $description 描述<br>mixed $default 默認值 | Command | public |
| **setName**($name) | 設置指令名稱 | string $name | Command | public |
| getName() | 獲取指令名稱 | void | String 指令名稱 |public |
| **setDescription**($description) | 設置描述 | string $description | Command |public |
| getDescription() | 獲取描述 | void | string 描述 |public |
| setHelp($help) | 設置幫助信息 | string $help | Command |public |
| getHelp() | 獲取幫助信息 | void | string 設置的幫助信息 |public |
| getProcessedHelp() | 獲取描述信息 | void | string 描述信息 |public |
| setAliases($aliases) | 設置別名 | string[] $aliases | Command |public |
| getAliases() | 獲取別名 | void | array |public |
| getSynopsis($short = false) | 獲取簡介 | bool $short 是否簡單的 | string 簡介 |public |
| addUsage($usage) | 添加用法介紹 | string $usage | Command |public |
| getUsages() | 獲取用法介紹 | void | array |public |
### **示例:**
~~~php
<?php
namespace app\common\command;
use think\Console;
use think\console\{Command,Output,Input};
use think\console\Input\{Argument,Option};
/*
* 帶參數腳本兼容linux定時和手動執行
*/
class Test extends Command
{
//定義任務名和描述
protected function configure(){
//設置參數
$this->addArgument('id',Argument::REQUIRED,'ID必填'); //必傳參數
$this->addArgument('name', Argument::OPTIONAL,'NAME選填');//可選參數
//選項定義
$this->addOption('type', 't', Option::VALUE_REQUIRED,'TYPE必填',0); //選項值必填
$this->addOption('status', 's', Option::VALUE_OPTIONAL,'STATUS選填',1); //選項值選填
$this->setName('test')->setDescription("php think test 測試"); //選項定義
}
//調用該類時,會自動運行execute方法
protected function execute(Input $input, Output $output){
set_time_limit(0);
@ob_end_clean();
@ob_implicit_flush(1);
ini_set('memory_limit','100M');
# echo substr_replace('18858281234','****',3,4);
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('Asia/Shanghai');
$excel_id = Cache::get('excel_id');
if(!empty($excel_id)){ //如果緩存有值,用定時腳本執行
$id = $excel_id;
}else{//如果使用命令行手動傳參執行
$args = $input->getArguments();//獲取傳參數組
print_r($args);
$options = $input->getOptions(); //獲取選項數組
print_r($options);
$output->writeln("參數接收為:".$args['id'].'--'.$args['name']);
//接收用全稱name,不能簡寫shortcut
$output->writeln("選項接收為:".$options['type'].'--'.$options['status']);
}
$page = 0;
while(true){
$page++;
$res = $model->where(['id'=>$id])->paginate(100, false, [ 'page' => $page, 'query' => $query ]); //獲取訂單數據
$res = json_decode($res,true);
if( empty($res['data']['body'])){//分頁獲取數據為空,則說明執行完畢
exit("已同步完成\n\r");//退出
}
//數據不為空,循環遍歷插入本地數據庫
foreach($res['data']['body'] as $k=>$v){
$data['product_id'] = $v['product_id'];
$data['order_status'] = $status;
Db::table(Config('database.prefix').'table')->insert($data);
}
echo "同步第{$page}頁完成\n\r"; //輸出
}
}
}
~~~
### **調用:**
#### 控制臺調用
~~~dart
# 四種調用結果相同
[root@root root]# php think test 1 admin --type "2" --status "3"
[root@root root]# php think test --type "2" --status "3" 1 admin
[root@root root]# php think test 1 admin -t"2" -s"3"
[root@root root]# php think test -t"2" -s"3" 1 admin
Array
(
[command] => test
[id] => 1
[name] => admin
)
Array
(
[type] => 2
[status] => 3
[help] =>
[version] =>
[quiet] =>
[verbose] =>
[ansi] =>
[no-ansi] =>
[no-interaction] =>
)
參數接收為:1--admin
選項接收為:2--3
[root@root root]#
~~~
#### 代碼調用
~~~php
namespace app\index\controller;
use think\Console;//引入Console
class Index{
$output = Console::call('command');#無參數調用
$output = Console::call('command',[args1,args2,argsN...]);#帶參數調用命令
$output = Console::call('test',['1','admin','-t2','-s3']);#帶參數調用(options和arguments不區分順序)
return $output->fetch(); #獲取輸出信息
}
~~~
## **Console**
### **方法**
| 屬性名 | 描述 |參數說明|返回值|權限|
| --- | --- | --- | --- | --- |
| __construct($name = 'UNKNOWN', $version = 'UNKNOWN', $user = null) | | | | |
| setUser($user) | 設置執行用戶 | $user | void | public |
| init($run = true) | 初始化 Console | bool $run 是否運行 Console | int|Console |public、static|
| call($command, array $parameters = [], $driver = 'buffer') | 調用命令 | string $command<br>array $parameters<br>string $driver | Output |public、static|
| run() | 執行當前的指令 | void | int |public|
| doRun(Input $input, Output $output) | 執行指令 | Input $input 輸入<br>Output $output 輸出 | int |public|
| setDefinition(InputDefinition $definition) | 設置輸入參數定義 | InputDefinition $definition 輸入定義 | Console |public|
| getDefinition() | 獲取輸入參數定義 | void | InputDefinition |public|
| getHelp() | 獲取幫助信息 | void | string |public|
| setCatchExceptions($boolean) | 設置是否捕獲異常 | bool $boolean 是否捕獲 |Console|public|
| setAutoExit($boolean) | 設置是否自動退出 | bool $boolean 是否自動退出 | Console |public|
| getName() | 獲取名稱 | void | string |public|
| setName($name) | 設置名稱 | string $name 名稱 |Console|public|
| getVersion() | 獲取版本 | void | string |public|
| setVersion($version) | 設置版本 | string $version 版本信息 |Console |public|
| getLongVersion() | 獲取完整的版本號 | void | string |public|
| register($name) | 注冊一個指令 | string $name 指令名稱 | Command |public|
| addCommands(array $commands) | 批量添加指令 | Command[] $commands 指令實例 |Console|public|
| add(Command $command) | 添加一個指令 | Command $command 命令實例 | Command\|bool |public|
| get($name) | 獲取指令 | string $name 指令名稱 | Command |public|
| has($name) | 某個指令是否存在 | string $name 指令名稱 | bool |public|
| getNamespaces() | 獲取所有的命名空間 | void | array |public|
| findNamespace($namespace) | 查找注冊命名空間中的名稱或縮寫 | string $namespace | string |public|
| find($name) | 查找指令 | string $name 名稱或者別名 | Command |public|
| all($namespace = null) | 獲取所有的指令 | string $namespace 命名空間 | Command[] |public|
| getAbbreviations($names) | 獲取可能的指令名 | array $names 指令名 | array |public|
| configureIO(Input $input, Output $output) | 配置基于用戶的參數和選項的輸入和輸出實例 | Input $input 輸入實例<br>Output $output 輸出實例 | void |public,protected|
| doRunCommand(Command $command, Input $input, Output $output) | 執行指令 | Command $command 指令實例<br>Input $input 輸入實例<br>Output $output 輸出實例| int |public,protected|
| getCommandName(Input $input) | 獲取指令的名稱 | Input $input 輸入實例 | string |public,protected|
| etDefaultInputDefinition() | 獲取默認輸入定義 | void | InputDefinition |public,protected|
| getDefaultCommands() | 獲取默認命令 | void | Command[] |public,protected|
| addDefaultCommands(array $classes) | 添加默認指令 | array $classes 指令 | void |public,static|
| extractNamespace($name, $limit = null) | 返回指令的命名空間部分 | string $name 指令名稱<br>string $limit 部分的命名空間的最大數量 | string |public|
| setDefaultCommand($commandName) | 設置默認的指令 | string $commandName 指令名稱 | Console |public|
## **Output**
### **常量**
| 常量名 | 描述 |參數類型|默認值|
| --- | --- | --- | --- |
| VERBOSITY_QUIET | | | 0 |
| VERBOSITY_NORMAL | | | 1 |
| VERBOSITY_VERBOSE | | | 2 |
| VERBOSITY_VERY_VERBOSE | | | 3 |
| VERBOSITY_DEBUG | | | 4 |
| OUTPUT_NORMAL | | | 0 |
| OUTPUT_RAW | | | 1 |
| OUTPUT_PLAIN | | | 2 |
### **方法**
實例化時傳入的think\console\output\driver\里的類(默認Console) 存入handle屬性,這里的方法差不多都是操作傳入的類的方法
| 屬性名 | 描述 |參數說明|返回值|權限|
| --- | --- | --- | --- | --- |
| __construct($driver = 'console') | | | |public|
| ask(Input $input, $question, $default = null, $validator = null) | | | |public|
| askHidden(Input $input, $question, $validator = null) | | | |public|
| confirm(Input $input, $question, $default = true) | | | |public|
| choice(Input $input, $question, array $choices, $default = null) | 窗口輸出時需要接受手動輸入值,如:$res=$output->choice($input,"請選擇正確答案",[1,2,3,4]); if($res==2){$output->write("恭喜你,回答正確!!!");}<br> | | |public|
| askQuestion(Input $input, Question $question) | | | |public,protected|
| block($style, $message) | 調用的是進一步封裝的newLine方法 | | |public,protected|
| newLine($count = 1) | 輸出空行,調用的是進一步封裝的write方法 | int $count | |public|
| writeln($messages, $type = self::OUTPUT_NORMAL) | 輸出信息并換行,調用的是進一步封裝的write方法 | string $messages<br>int $type | |public|
| write($messages, $newline = false, $type = self::OUTPUT_NORMAL) | 輸出信息 | string $messages<br>bool $newline<br>int $type | |public|
| renderException(\Exception $e) | | | |public|
| setVerbosity($level) | | | |public|
| getVerbosity() | | | |public|
| isQuiet() | | | |public|
| isVerbose() | | | |public|
| isVeryVerbose() | | | |public|
| isDebug() | | | |public|
| describe($object, array $options = []) | | | |public|
| __call($method, $args) | | | |public|
- 目錄結構與基礎
- 修改數據后頁面無變化
- 防跨目錄設置
- input
- 系統目錄
- 自動生成的文件以及目錄
- 類自動加載
- url生成
- 數據增刪改查
- 增加數據
- 數據更新
- 數據刪除
- 數據查詢
- 架構
- 生命周期
- 入口文件
- URL訪問規則
- 配置
- 默認慣例配置配置
- 初始應用配置
- 路由
- 域名路由
- URL生成
- 數據庫操作
- 方法列表
- 連接數據庫
- 分布式數據庫
- 查詢構造器
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 查詢語法
- 聚合查詢(統計)
- 時間查詢
- 高級查詢
- 視圖查詢
- 子查詢
- 輔助查詢之鏈式操作
- where
- table
- alias
- field
- order
- limit
- page
- group
- having
- join
- union
- distinct
- lock
- cache
- comment
- fetchSql
- force
- bind
- partition
- strict
- failException
- sequence(pgsql專用)
- 查詢事件
- 事務操作
- 監聽SQL
- 存儲過程
- 數據集
- 控制器
- 跳轉和重定向
- 空控制器和空操作
- 分層控制器
- Rest控制器
- 資源控制器
- 自動定位控制器
- tp3的增刪改查
- 方法注入
- 模型
- 屬性方法一覽
- 類方法詳解
- Model
- 調用model不存在的屬性
- 調用model中不存在的方法
- 調用model中不存在的靜態方法
- hasOne
- belongsTo
- hasMany {Relation}
- belongsToMany
- hasManyThrough
- morphMany
- morphOne
- morphTo
- ::hasWhere {Query}
- ::has
- relationCount
- data 【model】
- setInc {integer|true}
- setDec {integer|true}
- save {integer | false}
- saveAll {array}
- delete {integer}
- ::get 查詢單條數據 {Model}
- ::all 查詢多條數據{Model [ ]}
- ::create 新增單條數據 {Model}
- ::update 更新單條數據 {Model}
- ::destroy {integer}
- ::scope {Query}
- getAttr {mixed}
- xxx
- append
- appendRelationAttr
- hidden
- visible
- except
- readonly
- auto
- together
- allowField
- isUpdate
- validate
- toCollection
- toJson
- toArray
- 定義
- 新增
- 更新
- 查詢
- 刪除
- 聚合
- 獲取器
- 修改器
- 時間戳
- 只讀字段
- 軟刪除
- 類型轉換
- 數據完成
- 查詢范圍
- 模型分層
- 數組訪問和轉換
- JSON序列化
- 事件
- 關聯
- 一對一關聯
- 主表一對一關聯
- 從表一對一關聯(相對關聯)
- 一對多關聯
- 主表定義一對多關聯
- 從表定義一對多關聯
- 遠程一對多
- 多對多關聯
- 多態關聯
- 動態屬性
- 關聯預載入with()
- 關聯統計
- N+1查詢
- 聚合模型
- Model方法集合
- 表單驗證
- 驗證器
- 驗證規則
- 錯誤信息
- 驗證場景
- 控制器驗證
- 模型驗證
- 內置規則
- 靜態調用
- 表單令牌
- Token身份令牌
- 視圖
- 模版
- 變量輸出
- 函數輸出
- Request請求參數
- 模板注釋及原樣輸出
- 三元運算
- 內置標簽
- 模板繼承
- 模板布局
- 日志
- 日志初始化
- 日志驅動
- 日志寫入
- 獨立日志
- 日志清空
- 寫入授權
- 自定義日志
- 錯誤和調試
- 異常
- php系統異常及thinkphp5異常機制
- 異常處理
- 拋出異常
- 異常封裝
- resful
- 404頁面
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 性能調試
- 遠程調試
- 安全
- 輸入安全
- 數據庫安全
- 上傳安全
- 其它安全建議
- xss過濾
- 擴展
- 函數
- 類庫
- 行為
- 驅動
- Composer包
- Time
- 數據庫遷移工具
- Workerman
- MongoDb
- htmlpurifier XSS過濾
- 新浪SAE
- oauth2.0
- 命令行及生成文件
- 系統現成命令
- 創建類庫文件
- 生成類庫映射文件
- 生成路由緩存
- 清除緩存文件
- 生成配置緩存文件
- 生成數據表字段緩存
- 自定義命令行
- 開始
- 調用命令
- 雜項
- 助手函數
- URL重寫
- 緩存
- 緩存總結
- Session
- Cookie
- 多語言
- 分頁
- 上傳
- 驗證碼
- 圖像處理
- 文件處理
- 單元測試
- 自定義表單令牌