laravel创建自定义command命令笔记!
2020-09-24 本文已影响0人
DragonersLi
image.png
生成命令的模板文件
创建生成自定义的command命令:
php artisan make:command LogicCommand
在App\Console\Commands
下生成LogicCommand.php
App\Console\Command\LogicCommand.php
内容替换如下:
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class LogicCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:logic';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new logic class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Logic';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.DIRECTORY_SEPARATOR.'Stubs'.DIRECTORY_SEPARATOR.$this->type.'.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.DIRECTORY_SEPARATOR.'Logics';
}
}
生成命令的模板文件App\Console\Commands\Stubs\Logic.stub
:
<?php
// +----------------------------------------------------------------------
// | 逻辑层【逻辑层】
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------
// | Date:
// +----------------------------------------------------------------------
namespace DummyNamespace;
class DummyClass
{
/**
* 构造方法
* @return string
*/
public function __construct()
{
//just do it...
}
/**
* 列表
* @return string
*/
public function index()
{
//just do it...
}
/**
* 添加
* @return string
*/
public function create()
{
//just do it...
}
/**
* 存储
* @return string
*/
public function store()
{
//just do it...
}
/**
* 编辑
* @return string
*/
public function edit()
{
//just do it...
}
/**
* 保存
* @return string
*/
public function update()
{
//just do it...
}
/**
* 详情
* @return string
*/
public function show()
{
//just do it...
}
/**
* 删除
* @return string
*/
public function destroy()
{
//just do it...
}
}
App\Console\Kernel.php
:
protected $commands = [
//php artisan make:command commandName
Commands\LogicCommand::class,//创建逻辑层命令
];
php artisan make:logic TestLogic
根据模板生成自定义的命令:
<?php
// +----------------------------------------------------------------------
// | 逻辑层【逻辑层】
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------
// | Date:
// +----------------------------------------------------------------------
namespace App\Logics;
class HelloLogic
{
/**
* 构造方法
* @return string
*/
public function __construct()
{
//just do it...
}
/**
* 列表
* @return string
*/
public function index()
{
//just do it...
}
/**
* 添加
* @return string
*/
public function create()
{
//just do it...
}
/**
* 存储
* @return string
*/
public function store()
{
//just do it...
}
/**
* 编辑
* @return string
*/
public function edit()
{
//just do it...
}
/**
* 保存
* @return string
*/
public function update()
{
//just do it...
}
/**
* 详情
* @return string
*/
public function show()
{
//just do it...
}
/**
* 删除
* @return string
*/
public function destroy()
{
//just do it...
}
}