環(huán)境
- PHP 7
- Laravel 5.1
- OS X El Capitan 10.11.4
簡介
Artisan 是 Laravel 的命令行接口的名稱肘迎,它提供了許多實用的命令來幫助你開發(fā) Laravel 應用,它由強大的 Symfony Console 組件所驅動房匆。
文檔
目的
本文目的旨在實現(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('圖文郵件標題');
});
$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í)行效果分別如下顯示: