其他分享
首页 > 其他分享> > Laravel 自建 artisan 命令

Laravel 自建 artisan 命令

作者:互联网

# 还不会Laravel自建artisan命令?那你太low,下面和我全面的学习下吧。

# 如果 你不知道什么是laravel  artisan php 请去百度,以下是实用教程。

## 1.创建自定义命令文件
```php
php artisan make:command 你的的命令文件名字  eg:php artisan make:command SendEmails
```
#### php artisan make:command 你的的命令文件名字  eg:php artisan make:command SendEmails

## 2.打开Kernel文件注册你的命令(不要问我为什么打码,不想给你看)

![](https://cdn.learnku.com/uploads/images/202004/16/61195/nojWj2Syiz.png!large)

![](https://cdn.learnku.com/uploads/images/202004/16/61195/q8SW17El4y.png!large)

```php
protected $commands = [

    Commands\SendEmails::class

];
```
## 3.开始构建你的代码
```php

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = '你的命令名字  {--选项名字= :  帮助文档}';

eg:

protected $signature = 'sendEmails:generate  {--type= :  a生成商场文档;b生成Admin文档;}';

/**

* The console command description.

*

* @var string

*/

protected $description = '整体命令的介绍';

eg:

protected $description = '发送邮件';

/**

* Create a new command instance.

*

* @return void

*/
```

# 在handle里面书写你的命令代码
```php
public function handle()

{

    //如果你想要你的命令好看点,可以加上      $this->output->progressStart(1);    他会为你输出进度条,1为1个事件,你的命令里面做了几件事就输入多大的数字,当然你做了2件事,你也可以写为1或者3,这个决定于你。

    $message='这是你的消息提示';

    //获取输入的选项   $this->option('type')) type为选项名字

    if (($this->option('type'))=='a') {

    //如果你的type=a 就做什么事情

     $message='发送a事件邮件成功';

    } elseif (($this->option('type'))=='b') {

     //如果你的type=b 就做什么事情

     $message='发送b事件邮件成功';

    }

    // 这个可以把你的消息输入出来

    $this->comment($message);

}
```
## 现在你就可以构建一条属于自己的命令了。例如下面这样子的:

![](https://cdn.learnku.com/uploads/images/202004/16/61195/F88TdfUvrL.png!large)

标签:Laravel,##,命令,command,artisan,php,type
来源: https://www.cnblogs.com/king-jw/p/15040848.html