laravel 5.* 轻松实现Artisan的开发创建以及实现简单实例

Song4537 次浏览0个评论2017年07月18日

一. Artisan是什么?

ArtisanLaravel 中自带的命令行工具的名称。它提供了一些对您的应用开发有帮助的命令。它是由强大的 Symfony Console 组件驱动的。

二. Artisan应用场景:

我们知道了Artisan是什么?那我们来了解一下我们在什么地方会用到呢?我们主要讲解我们正常的开发过程!

  1. Laravel基本的命令
  2. 创建定时任务,比如每日凌晨运行脚本
  3. 执行处理数据量大,时间长的任务(比如管理员给某用户分配10万个订单)
  4. 批量调用接口,使用爬虫等!

三. 查看artisan命令列表,在项目下

php artisan list 
php artisan

四.创建artisan命令

php artisan make:console Test
# 5.3以后改为
php artisan make:command Test

执行完毕你会发现app/Console/Commands下多了一个Test.php文件夹(即可不用命令直接复制Test.php),我们更改$signature和$description,并且在handle下执行一些操作,文件如下,我已经加了描述

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';
    // 执行的命令,也可为order:test,这样就会创建一个order分组
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '这是一个测试artisan的描述';
    //命令的描述,建议中文,方便查找
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        echo "artisan test is ok";   //输出
        $this->info('artisan test is ok');  //输出绿色信息
        $this->error('Something went wrong!'); //输出红色背景错误信息
        $this->line('Display this on the screen');  //输出普通内容,不着色
    }
}

五.注册artisan,注册是在app/Console/Kernel.php中$commands注册的,如下我们注册test

<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\Test::class
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')
                 ->hourly();
    }
}

六. 使用artisan,现在我们在文件夹下执行php artisan命令,我们可以发现出现我们定义的test命令

Php artisan

执行

php artisan test

七.其他调用

a.在代码中调用Artisan命令
# 路由调用ARTISAN
Route::get('/foo', function () {
    Artisan::queue('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
});

# 程序内部调用 Artisan 命令
$exitCode = Artisan::call('config:cache');
b.在其他artisan中调用Artisan命令
public function handle(){
    $this->call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
    //}

会发现输出 artisan test is ok,OK,我们的artisan创建成功了,你的需求肯定不就这么简单,那么接下来明天我们会出artisan 实现脚本的编写以及定时任务的执行!

八、给Artisan传递参数

有时候我们可以给artisan传递参数,比如我们传入配置信息,订单信息等等;主要有2种模式传递参数,可在signature种引入{par1}{--par2=},获取方法分别为argumentoption,接下来我们看一下具体实例:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;

class Commission extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daily {param1} {--param2=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Larave学习网每日更新';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        // 参数调用方法
        $param1 = $this->argument('param1');
        $param2 = $this->option('param2');
        $this->info($param1);
        $this->info($param2);
    }
}

接下来我们测试一下:

php artisan daily nnn --param2=bbb

接下来输出我们传递的内容。

提交评论

请登录后评论

用户评论

    当前暂无评价,快来发表您的观点吧...

更多相关好文

    当前暂无更多相关好文推荐...