2017 © Pedro Peláez
 

yii2-extension yii2-crud

CRUD extension for Yii2

image

strongsquirrel/yii2-crud

CRUD extension for Yii2

  • Thursday, May 5, 2016
  • by kotchuprik
  • Repository
  • 6 Watchers
  • 5 Stars
  • 50 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Yii2 CRUD

CRUD extension for Yii2., (*1)

Installation

Run the Composer command to install the latest stable version:, (*2)

composer require strongsquirrel/yii2-crud @stable

The extension has a base functional for creating CRUD with the following actions:, (*3)

  • index: list of models
  • create: create a model
  • update: update a model
  • delete: delete a model
  • view: view a model
  • search: search models

Usage

Simple way

Just create controller:, (*4)

use strongsquirrel\crud\CrudController;

class AwesomeController extends CrudController
{
    public $modelClass = Model::class;
}

or use a special trait:, (*5)

class AwesomeController extends YourController
{
   use strongsquirrel\crud\CrudTrait;

   public $modelClass = Model::class;
}

IndexAction

Declare the following in your controller:, (*6)

use strongsquirrel\crud\IndexAction;

public function actions()
{
    return [
        'index' => [
            'class' => IndexAction::className(),
            'modelClass' => MyModel::className(),
            'view' => 'index', // by default
            // 'prepareDataProvider' => [$this, 'prepareDataProvider'],
            // 'checkAccess' => [$this, 'checkAccess'],
        ],
    ];
}

View file index.php:, (*7)

echo GridView::widget([
    'dataProvider' => $dataProvider,
]);

CreateAction

Declare the following in your controller:, (*8)

use strongsquirrel\crud\CreateAction

public function actions()
{
    return [
        'create' => [
            'class' => CreateAction::className(),
            'modelClass' => MyModel::className(),
            'scenario' => MyModel::SCENARIO_DEFAULT, // by default
            'view' => 'create', // by default
            // 'findModel' => [$this, 'findModel'],
            // 'checkAccess' => [$this, 'checkAccess'],
            // 'afterSave' => [$this, 'onAfterSave'],
        ],
    ];
}

View file create.php:, (*9)

$form = ActiveForm::begin();
echo $form->field($model, 'title');
ActiveForm::end();

UpdateAction

Declare the following in your controller:, (*10)

use strongsquirrel\crud\UpdateAction

public function actions()
{
    return [
        'update' => [
            'class' => UpdateAction::className(),
            'modelClass' => MyModel::className(),
            'scenario' => MyModel::SCENARIO_DEFAULT, // by default
            'view' => 'update', // by default
            // 'findModel' => [$this, 'findModel'],
            // 'checkAccess' => [$this, 'checkAccess'],
            // 'afterUpdate' => [$this, 'onAfterSave'],
        ],
    ];
}

View file update.php:, (*11)

$form = ActiveForm::begin();
echo $form->field($model, 'title');
ActiveForm::end();

DeleteAction

Declare the following in your controller:, (*12)

use strongsquirrel\crud\DeleteAction

public function actions()
{
    return [
        'delete' => [
            'class' => DeleteAction::className(),
            'modelClass' => MyModel::className(),
            // 'findModel' => [$this, 'findModel'],
            // 'checkAccess' => [$this, 'checkAccess'],
            // 'afterDelete' => [$this, 'onAfterDelete'],
        ],
    ];
}

ViewAction

Declare the following in your controller:, (*13)

use strongsquirrel\crud\ViewAction

public function actions()
{
    return [
        'view' => [
            'class' => ViewAction::className(),
            'modelClass' => MyModel::className(),
            'view' => 'view', // by default
            // 'findModel' => [$this, 'findModel'],
            // 'checkAccess' => [$this, 'checkAccess'],
        ],
    ];
}

View file view.php:, (*14)

echo DetailView::widget([
    'model' => $model,
]);

SearchAction

Declare the following in your controller:, (*15)

use strongsquirrel\crud\SearchAction

public function actions()
{
    return [
        'index' => [
            'class' => SearchAction::className(),
            'modelClass' => MySearchModel::className(),
            'scenario' => MySearchModel::SCENARIO_DEFAULT, // by default
            'view' => 'search', // by default
            'formMethod' => SearchAction::FORM_METHOD_GET, // by default
            'searchMethod' => 'search', // by default
            'searchOptions' => [], // by default
            // 'checkAccess' => [$this, 'checkAccess'],
        ],
    ];
}

Add search method to your class model:, (*16)

class MySearchModel extends Model
{
    // ...

    /**
     * The search method.
     * Should return an instance of [[DataProviderInterface]].
     *
     * @param array $options your search options
     *
     * @return ActiveDataProvider
     */
    public function search(array $options = [])
    {
        $query = MyModel::find();
        if ($this->validate()) {
            $query->filterWhere($this->getAttributes());
        }

        if (!empty($options['active'])) {
            $query->andWhere(['status' => MyModel::STATUS_ACTIVE]);
        }

        return new ActiveDataProvider(['query' => $query]);
    }
}

View file search.php:, (*17)

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $filterModel,
]);

Recipes

Changing CrudTrait actions

namespace app\controllers;

use app\models\News;
use strongsquirrel\crud\CrudTrait;

class NewsController extends Controller
{
    use CrudTrait {
        actions as traitActions;
    }

    public $modelClass = News::class;

    public function actions()
    {
        $actions = $this->traitActions();

        unset($actions['view']);
        unset($actions['update']);
        unset($actions['delete']);
        unset($actions['search']);

        $actions['create']['afterSave'] = function () {
            return $this->redirect('index');
        };

        return $actions;
    }
}

Additional parameters in view

namespace app\controllers;

use app\models\City;
use app\models\User;
use strongsquirrel\crud\UpdateAction;
use strongsquirrel\crud\ViewAction;

class UsersController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'update' => [
                'class' => UpdateAction::className(),
                'modelClass' => User::className(),
                'params' => [
                    'cities' => [$this, 'getCities'],
                    'title' => 'Update User',
                ],
            ],
            'view' => [
                'class' => ViewAction::className(),
                'modelClass' => User::className(),
                'params' => function (User $model) {
                    return [
                        'posts' => function (User $model) {
                            return $model->posts;
                        },
                        'city' => $model->city,
                        'cities' => [$this, 'getCities'],
                    ];
                },
            ],
        ];
    }

    /**
     * @return City[]
     */
    public function getCities()
    {
        return City::findAll();
    }
}

License

The MIT License (MIT). See LICENSE.md for more information., (*18)

The Versions

05/05 2016

dev-master

9999999-dev

CRUD extension for Yii2

  Sources   Download

MIT

The Requires

 

yii2 crud action actions

13/12 2015

v3.1.0

3.1.0.0

CRUD extension for Yii2

  Sources   Download

MIT

The Requires

 

yii2 crud action actions

03/12 2015

v3.0.0

3.0.0.0

CRUD extension for Yii2

  Sources   Download

MIT

The Requires

 

yii2 crud action actions

05/11 2015

v2.2.0

2.2.0.0

  Sources   Download

MIT

The Requires

 

21/06 2015

v2.1.0

2.1.0.0

  Sources   Download

MIT

The Requires

 

11/06 2015

v2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

 

10/06 2015

v1.1.0

1.1.0.0

  Sources   Download

MIT

The Requires

 

02/06 2015

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires