在某些場(chǎng)景里,我們需要在命令行中調(diào)用代碼
- 首先,在applicationcommand目錄(目錄沒有則創(chuàng)建)下新建hello.php:
<?php namespace appcommand; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleinputArgument; use thinkconsoleinputOption; use thinkconsoleOutput; use thinkRequest; class hello extends Command { /** * 重寫configure * {@inheritdoc} */ protected function configure() { $this // 命令的名字("think" 后面的部分) ->setName('hello') ?????????//?配置一個(gè)參數(shù)?使用$input->getArgument('username')獲取 ?????????//?->addArgument('username') ?????????//?運(yùn)行?"php?think?list"?時(shí)的簡(jiǎn)短描述 ?????????->setDescription('定時(shí)任務(wù)微服務(wù).') ?????????//?運(yùn)行命令時(shí)使用?"--help"?選項(xiàng)時(shí)的完整命令描述 ?????????->setHelp("定時(shí)任務(wù)微服務(wù)?無參數(shù)"); ?} ?/** ??*??*?重寫execute ??*??*?{@inheritdoc} ??*?? ??*?@param?Input?$input ??*?@param?Output?$output ??*/ ?protected?function?execute(Input?$input,?Output?$output) ?{ ?????echo?'hello?world'; ?}}
- 修改application/command.php(沒有則創(chuàng)建)
<?php return [ "appcommandhello",];
- cd到項(xiàng)目根目錄,在命令行輸入
php?think?hello
- OK,成功調(diào)用
hello?world
相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END