承接 getsolaris/laravel-make-service 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

getsolaris/laravel-make-service

最新稳定版本:v1.1.3

Composer 安装命令:

composer require getsolaris/laravel-make-service

包简介

A MVCS pattern create a service command for Laravel 5+

README 文档

README

Latest Stable Version Total Downloads Monthly Downloads License PHP Version Require

A Laravel package that provides an Artisan command to generate service classes, implementing the MVCS (Model-View-Controller-Service) pattern in your Laravel applications.

Overview

This package simplifies the creation of service layer classes in Laravel applications. It helps you maintain clean architecture by separating business logic from controllers, making your code more maintainable, testable, and reusable.

Requirements

  • PHP 7.1 or higher
  • Laravel 5.6.34 or higher (supports up to Laravel 12)

Installation

Install the package via Composer:

composer require getsolaris/laravel-make-service --dev

The package will automatically register itself using Laravel's package discovery.

Usage

Basic Command Syntax

php artisan make:service {name} {--i : Create a service interface}

Creating a Service Class

To create a simple service class:

php artisan make:service UserService

This will create a service class at app/Services/UserService.php:

<?php

namespace App\Services;

class UserService
{
    //
}

Creating a Service with Interface

To create a service class with its corresponding interface:

php artisan make:service UserService --i

This will create two files:

  1. Service class at app/Services/UserService.php:
<?php

namespace App\Services;

use App\Services\Interfaces\UserServiceInterface;

class UserService implements UserServiceInterface
{
    //
}
  1. Interface at app/Services/Interfaces/UserServiceInterface.php:
<?php

namespace App\Services\Interfaces;

interface UserServiceInterface
{
    //
}

Practical Examples

Example Service Implementation

<?php

namespace App\Services;

use App\Models\User;
use App\Services\Interfaces\UserServiceInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserService implements UserServiceInterface
{
    /**
     * Create a new user
     *
     * @param array $data
     * @return User
     */
    public function createUser(array $data): User
    {
        return DB::transaction(function () use ($data) {
            return User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
            ]);
        });
    }

    /**
     * Update user information
     *
     * @param User $user
     * @param array $data
     * @return bool
     */
    public function updateUser(User $user, array $data): bool
    {
        if (isset($data['password'])) {
            $data['password'] = Hash::make($data['password']);
        }

        return $user->update($data);
    }

    /**
     * Get user statistics
     *
     * @param User $user
     * @return array
     */
    public function getUserStatistics(User $user): array
    {
        return [
            'posts_count' => $user->posts()->count(),
            'comments_count' => $user->comments()->count(),
            'last_login' => $user->last_login_at,
        ];
    }
}

Using Services in Controllers

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Services\UserService;
use Illuminate\Http\JsonResponse;

class UserController extends Controller
{
    protected UserService $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    public function store(StoreUserRequest $request): JsonResponse
    {
        $user = $this->userService->createUser($request->validated());

        return response()->json([
            'message' => 'User created successfully',
            'user' => $user
        ], 201);
    }

    public function update(UpdateUserRequest $request, User $user): JsonResponse
    {
        $this->userService->updateUser($user, $request->validated());

        return response()->json([
            'message' => 'User updated successfully',
            'user' => $user->fresh()
        ]);
    }

    public function statistics(User $user): JsonResponse
    {
        $stats = $this->userService->getUserStatistics($user);

        return response()->json($stats);
    }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you discover any issues or have questions, please create an issue.

Author

License

This package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

  • Stars: 82
  • Watchers: 5
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固