搜索
首页php框架Laravel介绍Laravel自定义Make命令生成Service类

下面由laravel教程栏目给大家介绍Laravel自定义Make命令生成Service类,希望对需要的朋友有所帮助!

环境说明

我使用的环境是:Laravel Framework 8.40.0

C:\www\wwwroot\laravel8>php artisan --version
Laravel Framework 8.40.0

一、制作命令文件

前期知识的相关制作的教程,请参考我的另一篇博客Laravel自定义Make命令生成目标类。

  1. 运行如下命令

     php artisan make:command MakeService

    生成Console/Commands/MakeService.php命令文件。

  2. 修改继承类
    把继承类修改成GeneratorCommand,该类的命名空间为Illuminate\Console\GeneratorCommand
    删除实例化方法,handle函数
    实现一个方法getStub

  3. 设置name属性。
    修改$signature属性为name属性,并设置命令:

     protected $name = 'make:service';
  4. 设置type属性值
    type类型设置,我们生成的是service,所以我们设置的属性就是Service

     protected $type = 'Service';

    type类型是自己去定义的,本身没有特殊含义,可以不用设置。

    type属性值仅仅在创建错误的时候,给你一个友好的提示,如下所示:

     C:\www\wwwroot\laravel8>php artisan make:service TestService
     already exists!
    
     C:\www\wwwroot\laravel8>php artisan make:service TestService
     Service already exists!

    第一个是没有设置type属性的效果,第二个是设置了type属性的效果。

    官方使用的type有:Controller,Middleware,Cast,Channel…

    根据自己的需要修改其他的属性

  5. 设置Stub的位置和命令空间
    Stub的位置是在根目录下Stubs/service.stub里面。
    命名空间在app目录下Services里面。

实例代码如下:

<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeService extends GeneratorCommand{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = &#39;make:service&#39;;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;生成service对象类&#39;;

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = &#39;Service&#39;;

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        // Implement getStub() method.
        return $this->laravel->basePath('/stubs/service.stub');
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Services';
    }}

二、制作Stub文件

我的service文件目前不需要继承或者依赖什么类。所以,相对的比较简单。如果你有特别的需要,可以进行扩展操作。

实例代码如下:

<?phpnamespace DummyNamespace;class DummyClass{
    //}

DummyClassDummyNamespace在继承的GeneratorCommand类内部会被自动替换成自动生成的类名和设置的命名空间。

建议这种写法,可以使用编辑器的语法提示,获得更友好的提示效果。
另外,你也可以使用Larave内置的{{ class }}{{ namespace }}写法。

执行以下命令

php artisan make:service IndexService

能正常生成成功

C:\www\wwwroot\laravel8>php artisan make:service IndexService
Service created successfully.

生成的文件的目录是app/Services/IndexService.php,生成的文件如下:

<?php
namespace App\Services;
class IndexService{
    //}

相关推荐:最新的五个Laravel视频教程

以上是介绍Laravel自定义Make命令生成Service类的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:learnku。如有侵权,请联系admin@php.cn删除
包容的幻想:解决偏远工作中的孤立和孤独感包容的幻想:解决偏远工作中的孤立和孤独感Apr 25, 2025 am 12:28 AM

Tocombatisolationandlonelinessinremotework,companiesshouldimplementregular,meaningfulinteractions,provideequalgrowthopportunities,andusetechnologyeffectively.1)Fostergenuineconnectionsthroughvirtualcoffeebreaksandpersonalsharing.2)Ensureremoteworkers

Laravel用于全堆栈开发:综合指南Laravel用于全堆栈开发:综合指南Apr 25, 2025 am 12:27 AM

laravelispularfullull-stackDevelopmentBecapeitOffersAsAseAseAseAseBlendOfbackendEdpoperandPowerandForterFlexibility.1)ITSbackEndCapaPabilities,sightifyDatabaseInteractions.2)thebladeTemplatingEngingEngineAllolowsLows

视频会议摊牌:为远程会议选择正确的平台视频会议摊牌:为远程会议选择正确的平台Apr 25, 2025 am 12:26 AM

选择视频会议平台的关键因素包括用户界面、安全性和功能。1)用户界面应直观,如Zoom。2)安全性需重视,MicrosoftTeams提供端到端加密。3)功能需匹配需求,GoogleMeet适合简短会议,CiscoWebex提供高级协作工具。

哪些数据库版本与最新的Laravel兼容?哪些数据库版本与最新的Laravel兼容?Apr 25, 2025 am 12:25 AM

最新版本的Laravel10与MySQL5.7及以上、PostgreSQL9.6及以上、SQLite3.8.8及以上、SQLServer2017及以上兼容。这些版本选择是因为它们支持Laravel的ORM功能,如MySQL5.7的JSON数据类型,提升了查询和存储效率。

将Laravel用作全栈框架的好处将Laravel用作全栈框架的好处Apr 25, 2025 am 12:24 AM

laravelisanexceltentchoiceforfull-stackdevelopmentduetoitsRobustFeaturesAndEsofuse.1)ITSImplifiesComplexComplextaskSwithitSmodernphpsyNtaxandToolSandToolSlikeBlikeforFront-Endandeloquentormquentormquentormforback-end.2)

Laravel的最新版本是什么?Laravel的最新版本是什么?Apr 24, 2025 pm 05:17 PM

Laravel10,releasedonFebruary7,2023,isthelatestversion.Itfeatures:1)Improvederrorhandlingwithanewreportmethodintheexceptionhandler,2)EnhancedsupportforPHP8.1featureslikeenums,and3)AnewLaravel\Promptspackageforinteractivecommand-lineprompts.

最新的Laravel版本如何简化开发?最新的Laravel版本如何简化开发?Apr 24, 2025 pm 05:01 PM

thelatestlaravelververversionenhancesdevelopmentwith:1)简化的inimpliticmodelbinding,2)增强EnhancedeloquentcapabibilitionswithNewqueryMethods和3)改善了supportorfortormodernphpfortornphpforternphpfeatureserslikenamedargenamedArgonedArgonsemandArgoctess,makecodingMoreftermeforefterMealiteFficeAndEnjoyaigaigaigaigaigaiganigaborabilyaboipaigyAndenjoyaigobyabory。

在哪里可以找到最新的Laravel版本的发行说明?在哪里可以找到最新的Laravel版本的发行说明?Apr 24, 2025 pm 04:53 PM

你可以在laravel.com/docs找到最新Laravel版本的发布说明。1)发布说明提供了新功能、错误修复和改进的详细信息。2)它们包含示例和解释,帮助理解新功能的应用。3)注意新功能的潜在复杂性和向后兼容性问题。4)定期审查发布说明可以保持更新并激发创新。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。