PHP code example of strongsquirrel / yii2-crud

1. Go to this page and download the library: Download strongsquirrel/yii2-crud 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/ */

    

strongsquirrel / yii2-crud example snippets


use strongsquirrel\crud\CrudController;

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

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

   public $modelClass = Model::class;
}

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'],
        ],
    ];
}

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

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'],
        ],
    ];
}

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

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'],
        ],
    ];
}

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

use strongsquirrel\crud\DeleteAction

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

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'],
        ],
    ];
}

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

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'],
        ],
    ];
}

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]);
    }
}

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

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;
    }
}

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();
    }
}