目录
c) 在Kernel(app/Console/Kernel.php)类注册新增命令类
示例基于Laravel框架
a) 新增command类
可通过命令直接创建:php artisan make:command CallRole
可通过在目录复制修改:app/Console/Commands中复制已有的command类,修改名称与对应内容
b) 访问路由命令类内容
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
class CallRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:call {uri}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'php artisan route:call /route';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$request = Request::create($this->argument('uri'), 'GET');
$this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
}
}
c) 在Kernel(app/Console/Kernel.php)类注册新增命令类
<?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\CallRoute',
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
d) 执行路由(cli命令行模式)
php artisan route:call good/list