laravel 5.* 轻松实现Artisan的开发创建以及实现简单实例
Song •
4743 次浏览 •
0个评论 •
2017年07月18日
一. Artisan是什么?
Artisan
是 Laravel
中自带的命令行工具的名称。它提供了一些对您的应用开发有帮助的命令。它是由强大的 Symfony Console
组件驱动的。
二. Artisan应用场景:
我们知道了Artisan
是什么?那我们来了解一下我们在什么地方会用到呢?我们主要讲解我们正常的开发过程!
Laravel
基本的命令- 创建定时任务,比如每日凌晨运行脚本
- 执行处理数据量大,时间长的任务(比如管理员给某用户分配10万个订单)
- 批量调用接口,使用爬虫等!
三. 查看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 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=}
,获取方法分别为argument
和option
,接下来我们看一下具体实例:
<?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
接下来输出我们传递的内容。
-
laravel中distinct()的使用方法与去重 2017-09-11
-
Laravel将view缓存为静态html,laravel页面静态缓存 2021-10-09
-
[ laravel爬虫实战--基础篇 ] guzzle描述与安装 2017-11-01
-
[ 配置教程 ] 在ubuntu16.04中部署LNMP环境(php7+maridb且开启maridb远程以及nginx多域名访问 )并配置laravel环境 2017-07-18
-
mysql如何给运营人员添加只有查询权限的账号 2024-12-02
热门文章
-
mysql如何给运营人员添加只有查询权限的账号 2024-12-02
-
Mac 安装mysql并且配置密码 2024-11-20
-
阿里云不同账号(跨账号)ECS服务器同地域如何实现免费内网互通? 2024-11-12
-
electron安装使用better-sqlite3并解决NODE_MODULE_VERSION xxx. This version of Node.js requires 2024-11-06
-
Zerotier+Moon+Nginx实现内网穿透搭建网站 2024-08-23
更多相关好文