您好,欢迎访问一九零五行业门户网

聊聊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
<?phpnamespace {{ namespace }};class {{ class }}{ //}
我们的前期准备就此结束,是不是很简单?哈哈。
快速开始接下来我们就直接一把梭哈了,注意改动的代码噢。
我们主要是对着 servicemakecommand.php 文件一把梭哈,所以:
app/console/commands/servicemakecommand.php
<?phpnamespace 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
<?phpnamespace app\services;class userservice{ // }
恭喜,我们已经做到我们想要的结果了。
总结
虽然我们做得比较简陋,但我们只需稍加改进,就可以让它变得更完善。
以上就是聊聊laravel怎么快速生成 services?的详细内容。
其它类似信息

推荐信息