takeoo / laravel-service-layer
此包的最新版本(v0.2.2)没有提供许可证信息。
为 Laravel 应用提供轻量级服务层!
v0.2.2
2017-03-19 21:46 UTC
Requires
- php: >=7.0.0
- laravel/framework: >=5.1
This package is auto-updated.
Last update: 2024-09-29 05:05:51 UTC
README
为 Laravel 应用提供简单、轻量级的服务管理模块
安装
- 需要仓库
composer require takeoo/laravel-service-layer
-
将 \Takeoo\Service\TakeooServiceServiceProvider::class 添加到 config/app.php "providers" 数组中
-
运行
php artisan vendor:publish
使用方法
- 在项目中的任何位置创建新的服务类(例如 Example.php)
//Example.php namespace My\Service\Namespace; use Takeoo\Service; class MyService extends Service { // your code }
或者,如果您不想扩展 Service.php,只需使用 Service trait;
//Example.php namespace My\Service\Namespace; use Takeoo\Service\Traits; class MyService { use Service; // your code }
- 创建新服务类时,您必须注册它
- 前往 config/service.php
- 将您的服务添加到 "services" 数组中
'services' => [ 'Example' => \My\Service\Namespace\Example::class, ]
- 将 Service trait 添加到您的 Controller.php 类(如果您使用所有控制器扩展它)或到您想使用服务层的每个控制器类中
默认情况下,所有服务都作为单例创建,如果您想创建非单例类,请在 "service.non-singleton" 数组中提供其别名
代码
在代码中,您可以调用您的服务,如下所示
$service = $this->getService("Example");
如果您想使用自动完成(在 JetBrains IDE 中测试过),请在变量上方添加 PHPDoc
/** * @var \My\Service\Namespace\Example $serivce */ $service = $this->getService("Example");
或者,您始终可以为您常用的服务创建辅助函数,例如
/** * return \My\Service\Namespace\Example */ public function getExampleService() { return $this->getService("Example"); }