定制 raviyatechnical/laravel-service-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

raviyatechnical/laravel-service-generator

Composer 安装命令:

composer require raviyatechnical/laravel-service-generator

包简介

Generate services in Laravel with the artisan command

README 文档

README


Logo

Laravel Service Generator

Quickly generate services for your projects!



Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. The service pattern
  5. More generator packages
  6. Contributing
  7. License

Features

This package adds the php artisan make:service {name} command. The command generates an empty service class in app\Services to get started. I made this mainly for own use because I like to be able to generate recurring files from the command line to keep my workflow consistent.

Installation

Install the package with composer.

composer require raviyatechnical/laravel-service-generator

Usage

After installation the php artisan make:service {name} will be available in the list of artisan commands.

Generate Service

To generate a new service use the following artisan command.

php artisan make:service UserService

Generate a service for a model

Add a --service or -S param to generate a service for the model.

php artisan make:model Post --service

Use the -a or --all param to generate a service, migration, seeder, factory, policy, and resource controller for the model.

php artisan make:model Post --all

Generate a service for a controller

Add a --service or -S param to generate a service for the controller.

php artisan make:controller PostController --service

The service pattern

When to use the service pattern

A common question is: where do I put my business logic? You want to keep your models thin and your controller functions skinny. There are multiple ways to archive this, extracting your business logic to the service layer is a common method. By encapsulating your business logic in a service class you are able to re-use the logic for example in your controllers, commands, jobs and middelware.

How to use services

Once you have made a service it is time to add your business logic. We will discus how to use a service via static methods, dependency injection and how to use it with interfaces and repositories.

Static methods

a common way to use a service is to call it's methods statically. It is similar to helper functions. Let's say we have a PostService with a method to get a post based on a slug.

namespace App\Services;

use App\Models\Post;

class PostService
{
    // Declare the function as static
    public static function getPostBySlug(string $slug): Post
    {
        return Post::with('tags')
            ->where('slug', $slug)
            ->get();
    }
}

Next you can include the service class for example your controller and call the getPostBySlug method statically.

namespace App\Http\Controllers;

// Include the service
use App\Services\PostService;

class PostController extends Controller
{
    public function show(string $slug)
    {
        // Call the method statically from the service class
        $post = PostService::getPostBySlug($slug);
        
        return view('posts.show', compact('post'));
    }
}#

The getPostBySlug method is in this example a very simple function but as you can see it keeps you controller skinny and and your business logic seperated. Keep in mind that static classes and methods are stateless. The class won't save any data in itself.

Dependency Injection

Another popular method is to use services with dependency injection. With dependency injection you can write loosely coupled code. When done right this will improve the flexibility and maintainability of your code.

The PostService we used as example before will remain almost the same except we don't declare the functions inside the class as static anymore.

namespace App\Services;

use App\Models\Post;

class PostService
{
    public function getPostBySlug(string $slug): Post
    {
        return Post::with('tags')
            ->where('slug', $slug)
            ->get();
    }
}

Next we inject the service into the constructor of the class where we want to use it. Inside the constructor we assign the object to the $postService class property. Now the $postService property will be callable in all functions within the class with $this->postService. While typing your IDE will already typehint the functions in your PostService class, in this case only ->getPostBySlug($slug).

namespace App\Http\Controllers;

// Include the service
use App\Services\PostService;

class PostController extends Controller
{
    // Declare the property
    protected $postService;

    // Inject the service into the constructor
    public function __construct(PostService $postService)
    {
        // Assign the service instance to the class property
        $this->postService = $postService;
    }

    public function show($slug)
    {
        // Call the method you need from the service via the class property
        $post = $this->postService->getPostBySlug($slug);
        
        return view('posts.show', compact('post'));
    }
}

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

First Inspiration Package

https://github.com/timwassenburg/laravel-service-generator

License

The MIT License (MIT). Please see License File for more information.

raviyatechnical/laravel-service-generator 适用场景与选型建议

raviyatechnical/laravel-service-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「php」 「cli」 「pattern」 「laravel」 「services」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 raviyatechnical/laravel-service-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raviyatechnical/laravel-service-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 raviyatechnical/laravel-service-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-11