Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matthewpattell/yii2-services
Extension provide very simply use services for models and controllers
https://github.com/matthewpattell/yii2-services
Last synced: about 2 months ago
JSON representation
Extension provide very simply use services for models and controllers
- Host: GitHub
- URL: https://github.com/matthewpattell/yii2-services
- Owner: MatthewPattell
- License: mit
- Created: 2018-02-22T15:09:40.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-08T07:35:11.000Z (about 5 years ago)
- Last Synced: 2024-07-05T06:59:41.672Z (7 months ago)
- Language: PHP
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yii2-services
Extension provide very simply use services for models and controllers## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist matthew-p/yii2-services "*"
```or add
```
"matthew-p/yii2-services": "*"
```to the require section of your `composer.json` file.
## Usage
Once the extension is installed, simply use it in your code by:
Create service for model (must implement IService or extend any of services in MP\Services):
```php
use MP\Services\BaseModelService;/**
* ...
*
* @property SampleModel $model
*/
class MyCustomService extends BaseModelService
{
/**
* My simple method
*
* @return array
*/
public function getSampleMethod(): array
{
return [];
}
}
```Create model:
```php
...use MP\Services\ImplementServices;
/**
* Use services in model
* ...
*
* Services
* @property MyCustomService $customService
*/
class SampleModel extends ActiveRecord
{
use ImplementServices;
/**
* @inheritdoc
*/
public static function services(): array
{
return [
'customService' => MyCustomService::class,
];
}
...
}
```And use:
```php
$model = new SampleModel();
$model->customService->getSampleMethod();
```For controllers, everything is the same, only the service is inherited from **BaseControllerService**
## Tests
Run tests with command:
```bash
./vendor/bin/phpunit
```