使用 Laravel 編寫自定義命令

環(huán)境

  • PHP 7
  • Laravel 5.1
  • OS X El Capitan 10.11.4

簡介

Artisan 是 Laravel 的命令行接口的名稱,它提供了許多實用的命令來幫助你開發(fā) Laravel 應(yīng)用,它由強大的 Symfony Console 組件所驅(qū)動。

文檔

Laravel Artisan

目的

本文目的旨在實現(xiàn)三個簡單的命令,分別是優(yōu)化項目、清除緩存和發(fā)送郵件

實現(xiàn)步驟

新建命令

php artisan make:console LaravelOptimize --command=laravel:optimize
php artisan make:console LaravelClean --command=laravel:clean
php artisan make:console SendEmails --command=emails:send

注冊命令

打開文件 app/Console/Kernel.php,修改代碼如下:

protected $commands = [
    \App\Console\Commands\Inspire::class,
    \App\Console\Commands\SendEmails::class,
    \App\Console\Commands\LaravelClean::class,
    \App\Console\Commands\LaravelOptimize::class,
];

編寫命令執(zhí)行代碼

文件 SendEmails.php 代碼如下:

<?php

namespace App\Console\Commands;

use App\Facades\UserRepository;
use Illuminate\Console\Command;
use Mail;

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'emails:send';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'use SendCloud to send emails';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line("即將執(zhí)行郵件發(fā)送命令");

        if($this->confirm('確定執(zhí)行?')){
            $users = UserRepository::all();

            $bar = $this->output->createProgressBar(count($users));

            foreach ($users as $user) {
                $icon = "http://o93kt6djh.bkt.clouddn.com/Laravel-SendEmaillaravel-200x50.png";
                $image = "http://o93kt6djh.bkt.clouddn.com/Laravel-SendEmaillaravel-600x300.jpg";

                Mail::send('emails.test-image',
                    [
                        'name'  => $user->name,
                        'icon'  => $icon,
                        'image' => $image,
                    ],
                    function ($email) use ($user) {
                        $email->to($user->email)->subject('圖文郵件標(biāo)題');
                    });

                $bar->advance();
            }

            $bar->finish();

            $this->info("發(fā)送郵件完畢");
        } else {
            $this->info("取消執(zhí)行郵件發(fā)送命令");
        }
    }
}

文件 LaravelOptimize.php 代碼如下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class LaravelOptimize extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravel:optimize';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'optimize laravel project';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line("即將執(zhí)行優(yōu)化緩存命令");
        if($this->confirm('確定執(zhí)行?')){
            $this->line("開始執(zhí)行緩存命令");

            Artisan::call("config:cache");
            $this->info('Configuration cache cleared!');
            $this->info('Configuration cached successfully!');

            Artisan::call("route:cache");
            $this->info('Route cache cleared!');
            $this->info('Routes cached successfully!');

            Artisan::call("optimize");
            $this->info('Generating optimized class loader');

            if(function_exists('exec')){
                exec("composer dump-autoload");
            }

            Artisan::call("ide-helper:generate");
            $this->info('A new helper file was written to _ide_helper.php');

            $this->info("優(yōu)化緩存成功");
        } else {
            $this->info("取消執(zhí)行優(yōu)化緩存命令");
        }
    }
}

文件 LaravelClean.php 代碼如下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class LaravelClean extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravel:clean';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'clean project all cache';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line("即將執(zhí)行清除緩存命令");
        if($this->confirm('確定執(zhí)行?')){
            $this->line("開始執(zhí)行緩存命令");

            Artisan::call("clear-compiled");

            Artisan::call("auth:clear-resets");
            $this->info('Expired reset tokens cleared!');

            Artisan::call("cache:clear");
            $this->info('Application cache cleared!');

            Artisan::call("config:clear");
            $this->info('Configuration cache cleared!');

            Artisan::call("debugbar:clear");
            $this->info('Debugbar Storage cleared!');

            Artisan::call("route:clear");
            $this->info('Route cache cleared!');

            Artisan::call("view:clear");
            $this->info('Compiled views cleared!');

            $this->info("清除緩存成功");
        } else {
            $this->info("取消執(zhí)行清除緩存命令");
        }
    }
}

執(zhí)行效果

在終端分別執(zhí)行以下代碼:

php artisan laravel:optimize
php artisan laravel:clean
php artisan emails:send

執(zhí)行效果分別如下顯示:

php artisan laravel:optimize

執(zhí)行緩存清楚

php artisan laravel:clean

執(zhí)行緩存清楚

php artisan emails:send

執(zhí)行郵件發(fā)送
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文轉(zhuǎn)自CSDN,在原作者基礎(chǔ)上將老版本laravel的目錄更正為最新laravel5.4目錄,去除了不太好理解的...
    小小奶狗閱讀 2,146評論 6 10
  • 原文鏈接 必備品 文檔:Documentation API:API Reference 視頻:Laracasts ...
    layjoy閱讀 8,720評論 0 121
  • 先說幾句廢話,調(diào)和氣氛。事情的起由來自客戶需求頻繁變更,偉大的師傅決定橫刀立馬的改革使用新的框架(created ...
    wsdadan閱讀 3,203評論 0 12
  • 前言 終于有那么點時間能將Laravel 5的一些好的實踐總結(jié)出來,希望為普及Laravel和新的PHP編程思想出...
    該葉無法找到閱讀 6,359評論 0 47
  • 靜夜 音樂起 歌聲婉轉(zhuǎn)悲傷 飄蕩在空無的心間 想念 惆悵中 思念悠遠(yuǎn)綿長 纏綿于萬里的距離 你于我 是誰? 我于你...
    芮菈之城閱讀 211評論 0 0

友情鏈接更多精彩內(nèi)容