PHP code example of albertborsos / yii2-ddd

1. Go to this page and download the library: Download albertborsos/yii2-ddd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

albertborsos / yii2-ddd example snippets




namespace application\domains\app\business;

use application\domains\app\activerecords\AbstractApp;
use albertborsos\ddd\interfaces\BusinessObject;

class App extends AbstractApp implements BusinessObject
{
    // business logic
}



namespace application\services\app\forms;

use yii\base\Model;
use albertborsos\ddd\interfaces\FormObject;

class CreateAppForm extends Model implements FormObject
{
    public $name;
    public $languages;

    public function rules()
    {
        return [
            [['name', 'languages'], '



namespace application\services\app;

use albertborsos\ddd\models\AbstractService;
use application\services\app\forms\CreateAppLanguageForm;
use application\domains\app\business\App;
use yii\base\Exception;

class CreateAppService extends AbstractService
{
    /**
     * Business logic to store data for multiple resources.
     *
     * @return mixed
     */
    public function execute()
    {
        try {
            $model = new App();
            $model->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');
            }
        }
    }
}




namespace application\controllers;

use Yii;
use application\services\app\forms\CreateAppForm;
use application\services\app\CreateAppService;

class AppController extends \yii\web\Controller
{
    public function actionCreate()
    {
        $form = new CreateAppForm();
        
        if ($form->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,
        ]);
    }
}