定制 visiarch/laravel-service 二次开发

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

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

visiarch/laravel-service

最新稳定版本:1.0.0

Composer 安装命令:

composer require visiarch/laravel-service

包简介

A simple Laravel package to create service, using artisan commands

README 文档

README

laravel Service

laravel-service

Latest Stable Version License

"Buy Me A Coffee"

A Simple Package to create services, using artisan commands in laravel.

This package extends the make: commands to help you easily create service classes in Laravel 9+.

What is Service ?

A service is a component that is responsible for executing an application's business logic. This is a place to place logic that may be too complex or inappropriate to place in the controller.

Install

composer require visiarch/laravel-service

Once it is installed, you can use any of the commands in your terminal.

Usage

Services are used to separate business logic from the controller, making the controller leaner and focused on handling HTTP requests and providing appropriate responses.

With interface

php artisan make:service {name} --i

Without interface

php artisan make:service {name}

Examples

Create a service class with interface

php artisan make:service PostService --i

app/Services/Interfaces/PostServiceInterface.php

<?php

namespace App\Services\Interfaces;

/**
 * Interface PostServiceInterface
 *
 * This interface defines the methods for the PostService class.
 */

interface PostServiceInterface
{
   //
}

app/Services/PostService.php

<?php

namespace App\Services;

use App\Services\Interfaces\PostServiceInterface;

/**
 * Class PostService
 *
 * @package App\Services
 */

class PostService implements PostServiceInterface
{
   //
}

app\Providers\AppServiceProvider

public function register(): void
{
    //
    $this->app->bind(
    \App\Services\Interfaces\PostServiceInterface::class,
    \App\Services\PostService::class
    );
}

Create a service class without interface

php artisan make:service PostService

app/Services/PostService.php

<?php

namespace App\Services;

/**
 * Class PostService
 *
 * @package App\Services
 */

class PostService
{
   //
}

Implementation

With Interface

<?php

namespace App\Services\Interfaces;

use App\Models\Post;

class PostService implements PostServiceInterface
{
    public function store(array $data): Post;
}
<?php

namespace App\Services;

use App\Models\Post;

class PostService implements PostServiceInterface
{
    public function store(array $data): Post
    {
        return Post::create($data);
    }
}

Without Interface

<?php

namespace App\Services;

use App\Models\Post;

class PostService
{
    public function store(array $data): Post
    {
        return Post::create($data);
    }
}

How to implement it on the controller?

<?php

namespace App\Services;

use App\Models\Post;
use App\Interfaces\PostServiceInterface;
use App\Http\Requests\PostRequest;

class PostController extends Controller
{
    protected $postService;

    public function __construct(PostServiceInterface $postService)
    {
        $this->postService = $postService;
    }

    public function store(PostRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $this->postService->store($data);

        return redirect()->route('posts.index')->with('success', __('post created'));
    }
}
Just change PostServiceInterface to PostService if you are implementing without interface

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on any social media? Spread the word!

Thanks! visiarch

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-28