Laravel 自建 artisan 命令

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

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

1.创建自定义命令文件

php artisan make:command 你的的命令文件名字  eg:php artisan make:command SendEmails

php artisan make:command 你的的命令文件名字 eg:php artisan make:command SendEmails

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

protected $commands = [

     Commands\SendEmails::class

 ];

3.开始构建你的代码


 /**

 * 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里面书写你的命令代码

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);

}

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
chowjiawei
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

写的真好,文档也有

4年前 评论
chowjiawei (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!