matthew-p/yii2-services

Extension provide very simply use services for models and controllers

Installs: 1 325

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:yii2-extension

3.1 2019-07-29 08:32 UTC

This package is auto-updated.

Last update: 2024-04-08 16:47:01 UTC


README

Extension provide very simply use services for models and controllers

Installation

The preferred way to install this extension is through composer.

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):

use MP\Services\BaseModelService;

/**
 * ...
 *
 * @property SampleModel $model
 */
class MyCustomService extends BaseModelService
{
    /**
     * My simple method
     *
     * @return array
     */
    public function getSampleMethod(): array
    {
        return [];
    }
}

Create model:

...

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:

$model = new SampleModel();
$model->customService->getSampleMethod();

For controllers, everything is the same, only the service is inherited from BaseControllerService

Tests

Run tests with command:

./vendor/bin/phpunit