lambertns/laravel-make-service
Composer 安装命令:
composer require lambertns/laravel-make-service
包简介
Adds `php artisan make:service` command to generate service classes with optional automatic controller injection using PHP 8+ property promotion.
关键字:
README 文档
README
A Laravel package that adds a make:service command to generate service classes with optional dependency injection into controllers.
Why Use Services?
Controllers can quickly become bloated with business logic, making them hard to maintain and test. Services help by:
- 🔧 Separate Concerns: Move business logic out of controllers into dedicated service classes
- 📉 Reduce Controller Complexity: Keep controllers thin and focused on HTTP handling
- ♻️ Reusability: Share business logic across multiple controllers or queue jobs
- 🧪 Testability: Easier to unit test business logic in isolation
- 📚 Maintainability: Organized code that's easier to read and maintain
Example Problem
Without Services (Fat Controller):
class OrderController extends Controller { public function store(Request $request) { // Validation $validated = $request->validate([...]); // Business logic $order = Order::create($validated); // Payment processing $payment = PaymentGateway::charge(...); $order->update(['payment_id' => $payment->id]); // Notification logic Mail::to($order->user)->send(new OrderConfirmation($order)); // Inventory management foreach ($order->items as $item) { $item->product->decrement('stock', $item->quantity); } // More logic... return response()->json($order, 201); } }
With Services (Clean Controller):
class OrderController extends Controller { public function __construct( private OrderService $orderService ) {} public function store(Request $request) { $order = $this->orderService->createOrder($request->validated()); return response()->json($order, 201); } }
Features
- 🚀 Generate service classes with the
php artisan make:servicecommand - 💉 Automatically inject services into controllers using the
--controlleroption - 📝 Use PHP 8+ constructor property promotion for clean dependency injection
- 🎯 Add multiple methods to services using the
--methodsoption - ✅ Follows Laravel conventions and best practices
Installation
You can install the package via Composer:
composer require lambertns/laravel-make-service
That's it! The package will automatically register the service provider.
Quick Start
Let's say you have a controller with lots of business logic and want to refactor it:
- Create a service to extract the logic:
php artisan make:service PaymentService --controller=OrderController --methods="process,refund,cancel"
- Your service is created and automatically injected:
// app/Http/Services/PaymentService.php class PaymentService { public function process($order) { /* ... */ } public function refund($order) { /* ... */ } public function cancel($order) { /* ... */ } }
- Use it in your controller:
// app/Http/Controllers/OrderController.php class OrderController extends Controller { public function __construct( private PaymentService $paymentService ) {} public function store(Request $request) { // Now your business logic is in the service $order = $this->paymentService->process($request->validated()); return response()->json($order, 201); } }
Usage
Basic Service Creation
Create a simple service:
php artisan make:service UserService
This will create app/Http/Services/UserService.php:
<?php namespace App\Http\Services; class UserService { // Add your methods here }
Create Service with Methods
Add methods to your service:
php artisan make:service PaymentService --methods="pay,refund,cancel"
This creates a service with three methods:
<?php namespace App\Http\Services; class PaymentService { /** *pay() */ public function pay() { // TODO: implement pay() } /** *refund() */ public function refund() { // TODO: implement refund() } /** *cancel() */ public function cancel() { // TODO: implement cancel() } }
Auto-Inject into Controller
Automatically inject the service into a controller:
php artisan make:service PaymentService --controller=AuthController
This will:
- Create
app/Http/Services/PaymentService.php - Add
use App\Http\Services\PaymentService;to the controller - Add constructor injection with property promotion:
<?php namespace App\Http\Controllers; use App\Http\Services\PaymentService; class AuthController { public function __construct( private PaymentService $paymentService ) { } }
Inject into Multiple Controllers
Inject the service into multiple controllers at once (comma-separated):
php artisan make:service PaymentService --controller=AuthController,UserController,OrderController
This will inject the service into all three controllers and show a summary:
✓ Injected into AuthController
✓ Injected into UserController
✓ Injected into OrderController
Summary: 3 successful, 0 failed
Inject into Existing Controller
You can also add services to controllers that already have dependencies:
php artisan make:service NotificationService --controller=UserController
If the controller already has a constructor with dependencies, the new service will be added:
public function __construct( private UserService $userService, private NotificationService $notificationService // ← Added automatically ) { }
Command Options
| Option | Description | Example |
|---|---|---|
--controller |
Controller(s) to inject into (comma-separated for multiple) | --controller=AuthController,UserController |
--methods |
Comma-separated list of method names | --methods="index,store,update" |
--force |
Overwrite existing service | --force |
Examples
Create a service with multiple methods and inject into multiple controllers:
php artisan make:service OrderService --methods="create,update,cancel" --controller=OrderController,CheckoutController
This will create the OrderService with three methods and inject it into both controllers.
Use in nested namespaces:
php artisan make:service Admin\UserService --controller=Admin\UserController
Requirements
- PHP >= 8.1
- Laravel >= 10.0
- Illuminate/Support >= 10.0
- Illuminate/Console >= 10.0
- Illuminate/Filesystem >= 10.0
Features
Constructor Property Promotion
This package uses PHP 8's constructor property promotion for clean dependency injection:
public function __construct( private PaymentService $paymentService ) { }
This eliminates the need for separate property declarations and manual assignment.
Smart Use Statement Insertion
The package intelligently adds use statements in the correct location:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Services\PaymentService; // ← Added automatically class AuthController { // ... }
Troubleshooting
Service not being injected?
- Make sure the controller exists
- Check that the controller has a valid namespace
- Verify the controller file is writable
License
This package is open-sourced software licensed under the MIT license.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
lambertns/laravel-make-service 适用场景与选型建议
lambertns/laravel-make-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「command」 「service」 「dependency-injection」 「laravel」 「artisan」 「refactoring」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lambertns/laravel-make-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lambertns/laravel-make-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lambertns/laravel-make-service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
Shell command module for PHP.
Supporing Symfony's DependencyInjection component for your symfony1 project
A fast and intuitive dependency injection container.
Library with classes to help you do batch.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-26