Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/albertborsos/yii2-ddd
Domain-Driven Design inspired workflow for Yii 2.0 Framework
https://github.com/albertborsos/yii2-ddd
Last synced: 1 day ago
JSON representation
Domain-Driven Design inspired workflow for Yii 2.0 Framework
- Host: GitHub
- URL: https://github.com/albertborsos/yii2-ddd
- Owner: albertborsos
- Created: 2018-01-23T15:00:01.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-05-29T14:31:40.000Z (6 months ago)
- Last Synced: 2024-10-30T01:58:59.651Z (15 days ago)
- Language: PHP
- Homepage:
- Size: 370 KB
- Stars: 20
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/albertborsos/yii2-ddd.svg?branch=master)](https://travis-ci.org/albertborsos/yii2-ddd)
[![Coverage Status](https://coveralls.io/repos/github/albertborsos/yii2-ddd/badge.svg)](https://coveralls.io/github/albertborsos/yii2-ddd)DDD Classes for Yii 2.0
=======================
Classes for a Domain-Driven Design inspired workflow with Yii 2.0 FrameworkInstallation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Run
```
php composer.phar require --prefer-dist albertborsos/yii2-ddd
```to the require section of your `composer.json` file.
Usage
-----Lets see an example with a standard `App` model which is an implementation of `\yii\db\ActiveRecord` and generated via `gii`.
I recommend to do not make any modification with this class, but make it `abstract` to prevent direct usages.Then create a business model which extends our abstract active record class and implements `BusinessObject` interface.
Every business logic will be implemented in this class.```php
['in', 'range' => ['en', 'de', 'hu']]],
];
}
}```
And a simple example for a `service`. Services are expecting that the values in the `FormObject` are valid values.
That is why it is just store the values. The validation will be handled in the controller.```php
load($this->getForm()->attributes, '');if ($model->save()) {
$this->assignLanguages($model->id, $this->getForm()->languages);
$this->setId($model->id);return true;
}
} catch(\yii\db\Exception $e) {
$this->getForm()->addErrors(['exception' => $e->getMessage()]);
}
return false;
}private function assignLanguages($appId, $languageIds)
{
foreach ($languageIds as $languageId) {
$form = new CreateAppLanguageForm([
'app_id' => $appId,
'language_id' => $languageId,
]);if ($form->validate() === false) {
throw new Exception('Unable to validate language for this app');
}$service = new CreateAppLanguageService($form);
if ($service->execute() === false) {
throw new Exception('Unable to save language for this app');
}
}
}
}```
And this is how you can use it in the controller
```php
load(Yii::$app->request->post()) && $form->validate()) {
$service = new CreateAppService($form);
if ($service->execute()) {
AlertWidget::addSuccess('App created successfully!');
return $this->redirect(['view', 'id' => $service->getId()]);
}
}return $this->render('create', [
'model' => $form,
]);
}
}```