Basic CRUD operations in the Yii2 framework.
- Install the module via Composer:
composer require milan-miscevic/yii2crud
- Enable the module in the web.php config file:
'bootstrap' => [
// ...
'yii2crud',
],
- In the same file as above, define entities and namespaces for CRUD:
'modules' => [
// ...
'yii2crud' => [
'class' => \mmm\yii2crud\Module::class,
'cruds' => [
'article',
],
'namespaces' => [
'form' => 'app\form',
'models' => 'app\models',
'service' => 'app\service',
],
],
],
- Create form and active record files:
// form/ArticleForm.php
namespace app\form;
use mmm\yii2crud\CrudForm;
class ArticleForm extends CrudForm
{
public $title;
public $content;
public function rules()
{
return [
[['title', 'content'], 'required'],
];
}
}
// models/Article.php
namespace app\models;
use mmm\yii2crud\CrudActiveRecord;
class Article extends CrudActiveRecord
{
}