imjonos/laravel-base-service
Composer 安装命令:
composer require imjonos/laravel-base-service
包简介
Laravel base service
README 文档
README
A generic base service class for Laravel projects that provides a consistent and reusable way to handle business logic and data access. It integrates with laravel-base-repository and simplifies working with Eloquent models by encapsulating common operations like create, read, update, delete (CRUD), pagination, and more. The package also includes debugging capabilities through the DebugInterface and Debuggable trait, allowing for enhanced troubleshooting during development.
🧩 Overview
This package provides an abstract BaseService class that wraps around a repository and offers a clean interface for handling business logic in a structured and testable way. It is designed to be used in conjunction with the laravel-base-repository package, but it can also work with any custom repository implementing the required interface.
🛠 Installation
Install the package via Composer:
composer require imjonos/laravel-base-service
✅ This package depends on
imjonos/laravel-base-repository. Make sure it is installed in your project as well.
✅ Usage
1. Create Your Service Class
Create a new service class that extends BaseService and specifies the repository class:
namespace App\Services; use App\Repositories\OrderRepository; use Nos\BaseService\BaseService; class OrderService extends BaseService { protected string $repositoryClass = OrderRepository::class; }
2. Use the Service in a Controller or Other Logic
Inject the service and use its methods:
namespace App\Http\Controllers; use App\Services\OrderService; use Illuminate\Http\Request; class OrderController extends Controller { protected $orderService; public function __construct(OrderService $orderService) { $this->orderService = $orderService; } public function index() { $orders = $this->orderService->all(); return view('orders.index', compact('orders')); } public function store(Request $request) { $order = $this->orderService->create($request->all()); return redirect()->route('orders.show', $order->id); } }
🔧 Available Methods
| Method | Description |
|---|---|
getRepository() |
Returns the repository instance |
all() |
Get all records |
count() |
Count all records |
find(int $modelId) |
Find a record by ID |
exists(int $modelId) |
Check if a record exists |
create(array $data) |
Create a new record (throws exception on failure) |
update(int $modelId, array $data) |
Update a record by ID |
delete(int $modelId) |
Delete a record by ID |
updateOrCreate(array $attributes, array $data) |
Update or create a record |
paginate(int $pageNumber, int $pageSize, callable $builderCallback) |
Paginate results with optional query builder callback |
🌐 Project Structure
vendor/
└── imjonos/
└── laravel-base-service/
├── src/
│ └── BaseService.php
📦 Requirements
- PHP 8.0+
- Laravel 9+
🧪 Testing
You can easily mock the service and its repository in your tests, which helps keep your application logic decoupled and improves test coverage.
📝 License
This package is open-sourced software licensed under the MIT license. Please see the license file for more information.
🚀 Contributing
Please see contributing.md for details and a todolist.
imjonos/laravel-base-service 适用场景与选型建议
imjonos/laravel-base-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 915 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「service」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imjonos/laravel-base-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imjonos/laravel-base-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imjonos/laravel-base-service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Registry service.
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Service auto install for Nette Framework
Structure for the Laravel Service-Repository Pattern
Base Skeleton for Laravel Application
统计信息
- 总下载量: 915
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-27