getsolaris/laravel-make-service
Composer 安装命令:
composer require getsolaris/laravel-make-service
包简介
A MVCS pattern create a service command for Laravel 5+
README 文档
README
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:
- Service class at
app/Services/UserService.php:
<?php namespace App\Services; use App\Services\Interfaces\UserServiceInterface; class UserService implements UserServiceInterface { // }
- 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
- Solaris - getsolaris
- Email: getsolaris.kr@gmail.com
License
This package is open-sourced software licensed under the MIT license.
getsolaris/laravel-make-service 适用场景与选型建议
getsolaris/laravel-make-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 174.37k 次下载、GitHub Stars 达 81, 最近一次更新时间为 2018 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「service」 「laravel」 「contract」 「mvcs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 getsolaris/laravel-make-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 getsolaris/laravel-make-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 getsolaris/laravel-make-service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
A Laravel package for the Repository Design Pattern.
Registry service.
Inbox pattern process implementation for your Laravel Applications
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Service auto install for Nette Framework
统计信息
- 总下载量: 174.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 82
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-16