1、 简介

laravel和lumen提供了artisan命令行接口,以便我们来进行命令行操作。

我们可以通过php artisan list来查看框架为我们提供了哪些接口。

root@chen-Ubuntu:/data/test/lumen_5_2# php artisan list

Laravel Framework version Lumen (5.2.4) (Laravel Components 5.2.*)

Usage:

command [options] [arguments]

Options:

-h, --help Display this help message

-q, --quiet Do not output any message

-V, --version Display this application version

--ansi Force ANSI output

--no-ansi Disable ANSI output

-n, --no-interaction Do not ask any interactive question

--env[=ENV] The environment the command should run under.

-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:

help Displays help for a command

list Lists commands

migrate Run the database migrations

cache

cache:clear Flush the application cache

db

db:seed Seed the database with records

make

make:migration Create a new migration file

migrate

migrate:install Create the migration repository

migrate:refresh Reset and re-run all migrations

migrate:reset Rollback all database migrations

migrate:rollback Rollback the last database migration

migrate:status Show the status of each migration

queue

queue:failed List all of the failed queue jobs

queue:flush Flush all of the failed queue jobs

queue:forget Delete a failed queue job

queue:listen Listen to a given queue

queue:restart Restart queue worker daemons after their current job

queue:retry Retry a failed queue job

queue:work Process the next job on a queue

schedule

schedule:run Run the scheduled commands

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

root@chen-Ubuntu:/data/test/lumen_5_2# php artisan list

LaravelFrameworkversionLumen(5.2.4)(LaravelComponents5.2.*)

Usage:

command[options][arguments]

Options:

-h,--helpDisplaythishelpmessage

-q,--quietDonotoutputanymessage

-V,--versionDisplaythisapplicationversion

--ansiForceANSIoutput

--no-ansiDisableANSIoutput

-n,--no-interactionDonotaskanyinteractivequestion

--env[=ENV]Theenvironmentthecommandshouldrununder.

-v|vv|vvv,--verboseIncreasetheverbosityofmessages:1fornormaloutput,2formoreverboseoutputand3fordebug

Availablecommands:

helpDisplayshelpforacommand

listListscommands

migrateRunthedatabasemigrations

cache

cache:clearFlushtheapplicationcache

db

db:seedSeedthedatabasewithrecords

make

make:migrationCreateanewmigrationfile

migrate

migrate:installCreatethemigrationrepository

migrate:refreshResetandre-runallmigrations

migrate:resetRollbackalldatabasemigrations

migrate:rollbackRollbackthelastdatabasemigration

migrate:statusShowthestatusofeachmigration

queue

queue:failedListallofthefailedqueuejobs

queue:flushFlushallofthefailedqueuejobs

queue:forgetDeleteafailedqueuejob

queue:listenListentoagivenqueue

queue:restartRestartqueueworkerdaemonsaftertheircurrentjob

queue:retryRetryafailedqueuejob

queue:workProcessthenextjobonaqueue

schedule

schedule:runRunthescheduledcommands

也可以用help命令来查看,如何使用命令。

root@chen-Ubuntu:/data/test/lumen_5_2# php artisan help cache:clear

Usage:

cache:clear []

Arguments:

store The name of the store you would like to clear.

Options:

-h, --help Display this help message

-q, --quiet Do not output any message

-V, --version Display this application version

--ansi Force ANSI output

--no-ansi Disable ANSI output

-n, --no-interaction Do not ask any interactive question

--env[=ENV] The environment the command should run under.

-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:

Flush the application cache

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

root@chen-Ubuntu:/data/test/lumen_5_2# php artisan help cache:clear

Usage:

cache:clear[]

Arguments:

storeThenameofthestoreyouwouldliketoclear.

Options:

-h,--helpDisplaythishelpmessage

-q,--quietDonotoutputanymessage

-V,--versionDisplaythisapplicationversion

--ansiForceANSIoutput

--no-ansiDisableANSIoutput

-n,--no-interactionDonotaskanyinteractivequestion

--env[=ENV]Theenvironmentthecommandshouldrununder.

-v|vv|vvv,--verboseIncreasetheverbosityofmessages:1fornormaloutput,2formoreverboseoutputand3fordebug

Help:

Flushtheapplicationcache

2、编写命令

laravel的话你可以直接使用命令行来创建命令。

php artisan make:console SayHello

1

2

phpartisanmake:consoleSayHello

你还可以使用--command参数来设置终端命令名。

php artisan make:console SayHello --command say:hello

1

2

phpartisanmake:consoleSayHello--commandsay:hello

这样app\Console\Commands目录下就多了SayHello这个类。

变量protected $signature = 'say:hello'; 就是刚刚设置的终端命令名。

变量protected $description = 'a test command, just say hello';是用来描述改名了作用的。

我们可以将我们想做的操作写入handle方法。

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

echo "hello world!!! \n";

}

1

2

3

4

5

6

7

8

9

10

/**

* Execute the console command.

*

* @return mixed

*/

publicfunctionhandle()

{

echo"hello world!!! \n";

}

接下来我们将该命令注册到App\Console\Kernel中就可以执行了。

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

// Commands\Inspire::class,

Commands\SayHello::class,

];

1

2

3

4

5

6

7

8

9

10

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected$commands=[

// Commands\Inspire::class,

Commands\SayHello::class,

];

通过php artisan list命令可以查看到,say:hello已经注册成功。

...

say

say:hello a test command, just say hello

...

1

2

3

4

5

...

say

say:helloatestcommand,justsayhello

...

执行命令

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

hello world!!!

1

2

3

4

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

helloworld!!!

3、交互命令

3.1、 自定义参数

我们可以通过以下方式进行传参。

protected $signature = 'say:hello {user}';

1

2

protected$signature='say:hello {user}';

还可以让该参数可选可不选,以及定义默认的可选参数值:

// 选项参数...

say:hello {user?}

// 带默认值的选项参数...

say:hello {user=cc}

1

2

3

4

5

// 选项参数...

say:hello{user?}

// 带默认值的选项参数...

say:hello{user=cc}

使用argument方法可以来获取参数

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

$user = $this->argument('user');

echo "hello world!!!, i'm {$user} \n";

}

1

2

3

4

5

6

7

8

9

10

11

12

/**

* Execute the console command.

*

* @return mixed

*/

publicfunctionhandle()

{

$user=$this->argument('user');

echo"hello world!!!, i'm {$user} \n";

}

通过php artisan say:hello cc来进行调用

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc

hello world!!!, i'm cc

1

2

3

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc

helloworld!!!,i'mcc

还有一种传出方式,可以通过--方式来进行传参。

protected $signature = 'say:hello {user} {--queue}';

1

2

protected$signature='say:hello {user} {--queue}';

这种方式我们需要用option方法来接收参数。

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

$user = $this->argument('user');

echo "hello world!!!, i'm {$user} \n";

$queue = $this->option('queue');

var_dump($queue);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/**

* Execute the console command.

*

* @return mixed

*/

publicfunctionhandle()

{

$user=$this->argument('user');

echo"hello world!!!, i'm {$user} \n";

$queue=$this->option('queue');

var_dump($queue);

}

如果--queue开关被传递,其值是true,否则其值是false:

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc --queue

hello world!!!, i'm cc

bool(true)

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc

hello world!!!, i'm cc

bool(false)

1

2

3

4

5

6

7

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc --queue

helloworld!!!,i'm cc

bool(true)

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc

hello world!!!, i'mcc

bool(false)

也可以将其改造成传值方式

say:hello {user} {--queue=}

1

2

say:hello{user}{--queue=}

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc --queue=default

hello world!!!, i'm cc

string(7) "default"

1

2

3

4

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc --queue=default

helloworld!!!,i'mcc

string(7)"default"

简写方式

say:hello {user} {--Q|queue=}

1

2

say:hello{user}{--Q|queue=}

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc -Q default

hello world!!!, i'm cc

string(7) "default"

1

2

3

4

5

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc -Q default

helloworld!!!,i'mcc

string(7)"default"

传数组

say:hello {user} {--Q|queue=} {id*} // argument 方式

// 或则

say:hello {user} {--Q|queue=} {--id*} // option 方式

1

2

3

4

say:hello{user}{--Q|queue=}{id*}// argument 方式

// 或则

say:hello{user}{--Q|queue=}{--id*}// option 方式

# argument 方式

public function handle()

{

$user = $this->argument('user');

echo "hello world!!!, i'm {$user} \n";

$queue = $this->option('queue');

var_dump($queue);

$id = $this->argument('id');

print_r($id);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# argument 方式

publicfunctionhandle()

{

$user=$this->argument('user');

echo"hello world!!!, i'm {$user} \n";

$queue=$this->option('queue');

var_dump($queue);

$id=$this->argument('id');

print_r($id);

}

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc -Q default 1 2 3

hello world!!!, i'm cc

string(7) "default"

Array

(

[0] => 1

[1] => 2

[2] => 3

)

1

2

3

4

5

6

7

8

9

10

11

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello cc -Q default 1 2 3

helloworld!!!,i'mcc

string(7)"default"

Array

(

[0]=>1

[1]=>2

[2]=>3

)

3.2、 输入提示

我们将命令改回say:hello不传参数形式。

输入提示分4种模式:

询问

密码

确认

选择

询问模式

public function handle()

{

$name = $this->ask('What is your name?');

echo $name;

}

1

2

3

4

5

6

publicfunctionhandle()

{

$name=$this->ask('What is your name?');

echo$name;

}

不输入内容,自定会进行重试操作。

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

What is your name?:

>

[ERROR] A value is required.

What is your name?:

> cc

cc

1

2

3

4

5

6

7

8

9

10

11

12

13

14

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Whatisyourname?:

>

[ERROR]Avalueisrequired.

Whatisyourname?:

>cc

cc

密码模式

$password = $this->secret('What is the password?');

echo $password;

1

2

3

$password=$this->secret('What is the password?');

echo$password;

密码模式输入字符不会再命令行中显示。但是需要开启exec的函数使用权。

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

What is the password?:

>

[ERROR] A value is required.

What is the password?:

>

123

1

2

3

4

5

6

7

8

9

10

11

12

13

14

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Whatisthepassword?:

>

[ERROR]Avalueisrequired.

Whatisthepassword?:

>

123

确认模式

if ($this->confirm('Do you wish to continue? [y|N]')) {

echo "ok \n";

} else {

echo "not ok \n";

}

1

2

3

4

5

6

if($this->confirm('Do you wish to continue? [y|N]')){

echo"ok \n";

}else{

echo"not ok \n";

}

默认为no,只有输入y、Y、yes、YES才算通过

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Do you wish to continue? [y|N] (yes/no) [no]:

>

not ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Do you wish to continue? [y|N] (yes/no) [no]:

> 1

not ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Do you wish to continue? [y|N] (yes/no) [no]:

> y

ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Do you wish to continue? [y|N] (yes/no) [no]:

> yes

ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Do you wish to continue? [y|N] (yes/no) [no]:

> Y

ok

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

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Doyouwishtocontinue?[y|N](yes/no)[no]:

>

notok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Doyouwishtocontinue?[y|N](yes/no)[no]:

>1

notok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Doyouwishtocontinue?[y|N](yes/no)[no]:

>y

ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Doyouwishtocontinue?[y|N](yes/no)[no]:

>yes

ok

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Doyouwishtocontinue?[y|N](yes/no)[no]:

>Y

ok

选择模式

第一种anticipate用户可以无视给出的提示,自己填写。

$name = $this->anticipate('What is your name?', ['Taylor', 'Dayle']);

echo $name, "\n";

1

2

3

$name=$this->anticipate('What is your name?',['Taylor','Dayle']);

echo$name,"\n";

第二种choice,用户必须选择提示中的选项,否则失败。

$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], false);

echo $name, "\n";

1

2

3

$name=$this->choice('What is your name?',['Taylor','Dayle'],false);

echo$name,"\n";

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

What is your name? [Taylor]:

[0] Taylor

[1] Dayle

> 1

Dayle

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

What is your name? [Taylor]:

[0] Taylor

[1] Dayle

> Dayle

Dayle

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

What is your name? [Taylor]:

[0] Taylor

[1] Dayle

>

Taylor

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

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Whatisyourname?[Taylor]:

[0]Taylor

[1]Dayle

>1

Dayle

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Whatisyourname?[Taylor]:

[0]Taylor

[1]Dayle

>Dayle

Dayle

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Whatisyourname?[Taylor]:

[0]Taylor

[1]Dayle

>

Taylor

3.3、 不同的输出样式

# 行样式

$this->info('Display this on the screen');

$this->error('Something went wrong!');

$this->line('Display this on the screen');

# 表格

$headers = ['Name', 'Age'];

$users = [

['aa', 11],

['bb', 22],

['cc', 33],

];

$this->table($headers, $users);

# 进度条

$this->output->progressStart(10);

for($i = 0; $i < 10; $i++) {

$this->output->progressAdvance();

sleep(1);

}

$this->output->progressFinish();

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

# 行样式

$this->info('Display this on the screen');

$this->error('Something went wrong!');

$this->line('Display this on the screen');

# 表格

$headers=['Name','Age'];

$users=[

['aa',11],

['bb',22],

['cc',33],

];

$this->table($headers,$users);

# 进度条

$this->output->progressStart(10);

for($i=0;$i<10;$i++){

$this->output->progressAdvance();

sleep(1);

}

$this->output->progressFinish();

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Display this on the screen # 绿字

Something went wrong! # 红底白字

Display this on the screen # 白字

+------+-----+

| Name | Age |

+------+-----+

| aa | 11 |

| bb | 22 |

| cc | 33 |

+------+-----+

10/10 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

1

2

3

4

5

6

7

8

9

10

11

12

13

14

root@chen-Ubuntu:/data/test/laravel_5_2# php artisan say:hello

Displaythisonthescreen# 绿字

Somethingwentwrong!# 红底白字

Displaythisonthescreen# 白字

+------+-----+

|Name|Age|

+------+-----+

|aa|11|

|bb|22|

|cc|33|

+------+-----+

10/10[▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓]100%

4、调用command

我们可以在routes.php中去调用命令。当然,这只是个例子,你可以在任何地方调用它。

Route::get('/foo', function () {

$exitCode = Artisan::call('say:hello');

# $exitCode = Artisan::call('say:hello', ['user' => 'cc', '--queue' => 'default']); // 参数

var_dump($exitCode);

});

1

2

3

4

5

6

Route::get('/foo',function(){

$exitCode=Artisan::call('say:hello');

# $exitCode = Artisan::call('say:hello', ['user' => 'cc', '--queue' => 'default']); // 参数

var_dump($exitCode);

});

lumen php命令,laravel/lumen —— Artisan Console 命令行相关推荐

  1. laravel中artisan工具(命令)的使用详解

    artisan工具,首先,这个是一个php文件,它放在我们laravel框架的根目录 Artisan工具简介 Artisan 是 Laravel 中自带的命令行工具的名称.它提供了一些对您的应用开发有 ...

  2. lumen php命令,laravel and lumen 软删除操作

    知识都是有联系的,这绝对是真理.作为一名小白,看了一点官方文档,把我自己理解的软删除操作给大家讲讲.有些就是套用官方文档的话. 定义:什么是软删除呢,所谓软删除指的是数据表记录并未真的从数据库删除,而 ...

  3. laravel 如何 new php 类,PHP实例:laravel通过创建自定义artisan make命令来新建类文件详解...

    <PHP实例:laravel通过创建自定义artisan make命令来新建类文件详解>要点: 本文介绍了PHP实例:laravel通过创建自定义artisan make命令来新建类文件详 ...

  4. laravel 创建自定义的artisan make命令来新建类文件

    前言 我们在laravel开发时经常用到artisan make:controller等命令来新建Controller.Model.Job.Event等类文件. 在Laravel5.2中artisan ...

  5. Laravel自定义artisan命令在Sell中运行

    Laravel自定义artisan命令在Sell中运行 Artisan 是 Laravel 自带的命令行接口,它为我们在开发过程中提供了很多有用的命令. Console目录 app/Console 目 ...

  6. Laravel artisan常用命令集锦

    1.控制器 or Model // 5.2版本创建一个空控制器 php artisan make:controller BlogController // 创建Rest风格资源控制器 php arti ...

  7. php artisan migrate,Laravel php artisan 自动生成Model+Migrate+Controller 命令大全

    php artisan 命令是Laravel框架自带的命令,方便用户快速创建.查看对应的模块参数等. 一.常用的命令: php artisan list                        ...

  8. Laravel/Lumen搭建服务器性能测试

    背景和目的 一个最简单的服务器,我们至少也要关心服务器的吞吐量.cpu使用率.内存消耗.这篇文章会在上篇文章的基础上,使用搭建好的环境测试这个几个基本指标,分析性能瓶颈,给出可能的解决的方法.最终的目 ...

  9. php artisan命令表,php artisan 命令列表

    php  artisan 命令列表 命令获取 上面的翻译内容 命令说明备注 php artisan make:resource ?创建api返回格式化资源>=5.4版本可用 php artisa ...

最新文章

  1. iOS 2D绘图详解(Quartz 2D)之概述
  2. 洛谷P3295 [SCOI2016]萌萌哒(倍增+并查集)
  3. oracle通过sid远程登录,oracle怎么捕获用户登录信息,如SID,IP地址等
  4. 社区奖品之原木双面记事板
  5. 运行数据区②---堆
  6. 用python画三维图、某区域的高程,python - 在PyQt中绘制具有高程和降低效果的3D矩形/多边形 - SO中文参考 - www.soinside.com...
  7. 50行javaScript代码实现简单版的 call , apply ,bind 【中级前端面试基础必备】
  8. IAR下STM32进入HardFault_Handler
  9. php企业应用,PHP企业级应用缓存技术详解
  10. 仓储系统流程图_有效的仓储物流管理的6个重要提示
  11. 常见泰勒展开公式及复杂泰勒展开求法
  12. chrome误删书签恢复
  13. 华为员工去面试被淘汰后怒怼HR:华为出来的也能被拒,很无语
  14. 计算机无法登录到你的账户,Windows10系统提示“无法登录到你的账户”如何解决...
  15. Android 应用市场大全 主流市场
  16. webim【LayIM】开发者文档
  17. mysql注入实验报告_网络安全实验报告 第二章
  18. 文件大小单位Bytes, KB, MB, GB, TB, PB等及换算关系,英语怎么说?
  19. 通信:从功耗角度出发,5G相比4G,基站和终端功耗是降低了还是升高了?
  20. 高通芯片启动流程概要

热门文章

  1. 梦想起航商务工作PPT模板-优页文档
  2. 如何在注册表中删除用户帐户信息。
  3. Fabric.js 上划线、中划线(删除线)、下划线
  4. 扫盲教程:单片机IIC基础通信
  5. 【Java设计模式 面向对象设计思想】一 再谈面向对象和封装、抽象、继承、多态四大特性
  6. NetBackup 8.2 LinuxR 服务器安装及使用 ssh 方法将客户端软件从 UNIX 主服务器安装到 UNIX 客户端(持续更新)
  7. 我给新加坡华人送外卖,一趟5000块
  8. 【html+css+js】用前端做一个视频播放器页面
  9. 用TMS320c54x汇编语言求方差,TMS320C54x的指令.pdf
  10. 生活集思录-大学门外的事情