这篇文章主要给大家介绍了关于Laravel如何自定义command命令的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Laravel具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
前言
用过Laravel的都知道,Laravel通过php artisan make:controller可以生成控制器,同样的夜可以用命令生成中间介和模型,那怎么自定义生成文件呢?
下面话不多说了,来一起看看详细的介绍吧
自定义方法如下:
1.创建command类
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class ServiceMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:service';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new service class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Services';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/service.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace."\Services";
}
}
2.在Commands/stubs文件下创建自定义模板文件
<?php
namespace DummyNamespace;
class DummyClass
{
public function __construct()
{
}
}
创建了一个只有构造函数的类,具体模板可以自己定义
运行测试
php artisan make:service Web/TestService
这个时候Services文件下的Web目录下会生成TestService文件,Web目录不存在时会自动创建
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对编程学习网的支持。
织梦狗教程
本文标题为:Laravel如何自定义command命令浅析


基础教程推荐
猜你喜欢
- TP5 连接多个数据库及使用方法 2023-08-30
- PHP实现创建一个RPC服务操作示例 2023-04-01
- TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例 2023-01-19
- PHP实现生成数据字典功能示例 2022-10-18
- PHP删除数组中指定值的元素常用方法实例分析【4种方法】 2022-11-12
- thinkPHP3.2.2框架行为扩展及demo示例 2022-11-07
- php中使用array_filter()函数过滤数组实例讲解 2023-05-19
- PHP使用SMTP邮件服务器发送邮件示例 2022-11-16
- PHP数据加密方式梳理介绍 2023-07-03
- laravel model模型定义实现开启自动管理时间created_at,updated_at 2023-03-02