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'],
],
];
}
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();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.