jasco-b / view-model
View model for yii2
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2025-04-08 07:31:50 UTC
README
View model for Yii2 inspired from laravel view model
Installation
You can install the package via composer:
composer require jasco-b/view-model
Usage
Often in Yii2 or any other frameworks in CRUD`s, it is required from developer use the same data in create and update actions. For instance:
Old Controller
class PostController extends Controller { ... public function actionCreate() { $model = new Post(); $categories = Categories::find()->all(); ... return $this->render('create', [ 'model' => $model, 'categories' => $categories, ]); } public function actionUpdate($id) { $model = $this->findModel($id); $categories = Categories::find()->all(); ... return $this->render('update', [ 'model' => $model, 'categories' => $categories, ]); } }
As you have seen above, $categoreis has been used in Create and Update action. You can make clean controller and even you can add to view model your comlex logic.
View model
... use jascoB\ViewModel\ViewModel; class PostViewModel extends ViewModel { public $model; public function __construct($model) { $this->model = $model; } public function categories() { return $categories = Categories::find()->all(); } }
Controller
class PostController extends Controller { ... public function actionCreate() { $model = new Post(); ... return $this->render('create', new PostViewModel($model)); } public function actionUpdate($id) { $model = $this->findModel($id); ... return $this->render('update', new PostViewModel($model)); } }
In order use view model with Yii2 we should change config.php
return [ 'components' => [ 'view'=>[ 'class'=>'jascoB\ViewModel\Classes\View', ], ], ];