Laravel Artisan控制台
laravel框架提供了三种通过命令行进行交互的主要工具: artisan,ticker 和 repl 。本章详细解释了artisan。
工匠介绍
artisan是laravel经常使用的命令行界面,它包含一组用于开发web应用程序的有用命令。
例
以下是artisan中几个命令的列表以及它们各自的功能 -
启动laravel项目
php artisan serve
启用缓存机制
php artisan route:cache
查看artisan支持的可用命令列表
php artisan list
查看有关任何命令的帮助并查看可用的选项和参数
php artisan help serve
以下屏幕截图显示了上面给出的命令的输出 -

编写命令
除artisan中列出的命令外,用户还可以创建可在web应用程序中使用的自定义命令。请注意,命令存储在 app / console / commands目录中 。
下面显示了创建用户定义命令的默认命令 -
php artisan make:console
一旦你输入上面给出的命令,你可以看到输出如下面的屏幕截图所示 -

为 defaultcommand 创建的文件被命名为 defaultcommand.php ,如下所示 -
namespace app\console\commands;
use illuminate\console\command;
class defaultcommand extends command{
/**
* the name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* the console command description.
*
* @var string
*/
protected $description = 'command description';
/**
* create a new command instance.
*
* @return void
*/
public function __construct(){
parent::__construct();
}
/**
* execute the console command.
*
* @return mixed
*/
public function handle(){
//
}
} 该文件包含用户定义的命令的签名和说明。命名 句柄 的公共函数在执行命令时执行功能。这些命令在同一目录下的文件 kernel.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 = [
// commands\inspire::class,
commands\defaultcommand::class
];
/**
* define the application's command schedule.
*
* @param \illuminate\console\scheduling\schedule $schedule
* @return void
*/
protected function schedule(schedule $schedule){
// $schedule--->command('inspire')
// ->hourly();
}
} 请注意,给定命令的任务时间表是在名为 schedule 的函数中定义的,该函数包含一个用于计划 每小时 参数的任务的参数。
这些命令在命令数组中注册,其中包括命令的路径和名称。
一旦命令被注册,它就被列在artisan命令中。当您调用指定命令的帮助属性时,将显示签名和说明部分中包含的值。
让我们看看如何查看我们的命令 defaultcommand 的属性。你应该使用如下所示的命令 -
php artisan help defaultcommand


