聊聊laravel怎么快速生成 Services?
下面由Laravel教程栏目带大家介绍如何使用 make:service 命令来快速生成 Services,希望对大家有所帮助!
前言
Artisan 是 Laravel 附带的命令行接口。Artisan 以 artisan
脚本的形式存在于应用的根目录,并提供了许多有用的命令,这些命令可以在构建应用时为你提供帮助。
除 Artisan 提供的命令外,你也可以编写自己的自定义命令。 命令在多数情况下位于 app/Console/Commands 目录中; 不过,只要你的命令可以由 Composer 加载,你就可以自由选择自己的存储位置。
前期工作
在开始之前,我们要准备相应的目录和文件。
我们可以使用以下命令快速生成 ServiceMakeCommand.php
文件:
php artisan make:command ServiceMakeCommand
执行完后会在你的 Console
文件夹下生成 Commands
文件夹和 Commands/ServiceMakeCommand.php
文件。
我们还需要在 Commands
文件夹下添加一些文件夹和文件:
结构如下:
- app - Console + - Commands + - stubs + - service.plain.stub + - ServiceMakeCommand.php - Kernel.php - . - . - .
service.plain.stub
代码:
app/Console/Commands/stubs/service.plain.stub
<?php namespace {{ namespace }}; class {{ class }} { // }
我们的前期准备就此结束,是不是很简单?哈哈。
快速开始
接下来我们就直接一把梭哈了,注意改动的代码噢。
我们主要是对着 ServiceMakeCommand.php
文件一把梭哈,所以:
app/Console/Commands/ServiceMakeCommand.php
<?php namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class ServiceMakeCommand extends GeneratorCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'make:service {name}'; /** * The console command description. * * @var string */ protected $description = 'Create a new service class'; /** * The type of class being generated. * * @var string */ protected $type = 'Service'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__ . '/stubs/service.plain.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace ( $rootnamespace ) { return $rootnamespace . '\Services'; } }
最后,我们执行以下命令快速生成 UserService.php
文件:
php artisan make:service UserService
结构如下:
- app - Console - Commands - stubs - service.plain.stub - ServiceMakeCommand.php - Kernel.php + - Services + - UserService.php - . - . - .
让我们查看 UserService.php
和我们想象中的代码是否一致:
app/Services/UserService.php
<?php namespace App\Services; class UserService{ // }
恭喜,我们已经做到我们想要的结果了。
总结
虽然我们做得比较简陋,但我们只需稍加改进,就可以让它变得更完善。
以上是聊聊laravel怎么快速生成 Services?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Laravel - Artisan 命令 - Laravel 5.7 提供了处理和测试新命令的新方法。它包括测试 artisan 命令的新功能,下面提到了演示?

Laravel - 分页自定义 - Laravel 包含分页功能,可帮助用户或开发人员包含分页功能。 Laravel 分页器与查询构建器和 Eloquent ORM 集成。自动分页方法

Laravel邮件发送失败时的退信代码获取方法在使用Laravel开发应用时,经常会遇到需要发送验证码的情况。而在实�...

Laravel计划任务运行无响应排查在使用Laravel的计划任务调度时,不少开发者会遇到这样的问题:schedule:run...

在dcatadmin(laravel-admin)中如何实现自定义点击添加数据的表格功能在使用dcat...

Laravel - 转储服务器 - Laravel 转储服务器随 Laravel 5.7 版本一起提供。以前的版本不包括任何转储服务器。转储服务器将成为 laravel/laravel Composer 文件中的开发依赖项。

Laravel框架中Redis连接的共享与select方法的影响在使用Laravel框架和Redis时,开发者可能会遇到一个问题:通过配置...
