PHPackages                             jasco-b/view-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. jasco-b/view-model

ActivePackage[Templating &amp; Views](/categories/templating)

jasco-b/view-model
==================

View model for yii2

1.0.2(6y ago)622MITPHPCI failing

Since Sep 18Pushed 6y ago1 watchersCompare

[ Source](https://github.com/jasco-b/view-model)[ Packagist](https://packagist.org/packages/jasco-b/view-model)[ RSS](/packages/jasco-b-view-model/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

Yii2 view model
===============

[](#yii2-view-model)

View model for Yii2 inspired from [laravel view model](https://github.com/spatie/laravel-view-models)

Installation
============

[](#installation)

You can install the package via composer:

```
composer require jasco-b/view-model
```

Usage
=====

[](#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

[](#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

[](#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

[](#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',
        ],
    ],
];
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Total

3

Last Release

2336d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/682988?v=4)[joel](/maintainers/jasco)[@jasco](https://github.com/jasco)

---

Top Contributors

[![jasco-b](https://avatars.githubusercontent.com/u/12882799?v=4)](https://github.com/jasco-b "jasco-b (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jasco-b-view-model/health.svg)

```
[![Health](https://phpackages.com/badges/jasco-b-view-model/health.svg)](https://phpackages.com/packages/jasco-b-view-model)
```

###  Alternatives

[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3861.2M](/packages/limenius-react-bundle)[area17/laravel-auto-head-tags

Laravel Auto Head Tags helps you build the list of head elements for your app

4616.0k](/packages/area17-laravel-auto-head-tags)[jelix/wikirenderer

WikiRenderer is a library to generate HTML or anything else from wiki content.

1712.2k1](/packages/jelix-wikirenderer)[webkinder/sproutset

A Composer package for handling responsive images in Roots Bedrock + Sage + Blade projects.

281.8k](/packages/webkinder-sproutset)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
