Browse Source

basic crud模板开发

wechat_public_accounts
blobt 5 years ago
parent
commit
16a622e0e1
  1. 58
      common/models/Cat.php
  2. 9
      environments/skeleton/kcadmin/config/main-local.php
  3. 5
      kcadmin/views/category/index.php
  4. 179
      vendor/blobt/generators/crud/default/controller.php
  5. 86
      vendor/blobt/generators/crud/default/search.php
  6. 42
      vendor/blobt/generators/crud/default/views/_form.php
  7. 49
      vendor/blobt/generators/crud/default/views/_search.php
  8. 29
      vendor/blobt/generators/crud/default/views/create.php
  9. 105
      vendor/blobt/generators/crud/default/views/index.php
  10. 40
      vendor/blobt/generators/crud/default/views/update.php
  11. 58
      vendor/blobt/generators/crud/default/views/view.php

58
common/models/Cat.php

@ -0,0 +1,58 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "cat".
*
* @property int $id
* @property string $cat_name
* @property string $icon
* @property int $icon_type
* @property string $description
* @property int $sort_order
* @property int $created_at
* @property int $updated_at
*/
class Cat extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'cat';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['cat_name'], 'required'],
[['icon_type', 'sort_order', 'created_at', 'updated_at'], 'integer'],
[['description'], 'string'],
[['cat_name', 'icon'], 'string', 'max' => 64],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cat_name' => 'Cat Name',
'icon' => 'Icon',
'icon_type' => 'Icon Type',
'description' => 'Description',
'sort_order' => 'Sort Order',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}

9
environments/skeleton/kcadmin/config/main-local.php

@ -19,6 +19,15 @@ if (!YII_ENV_TEST) {
$config['bootstrap'][] = 'gii'; $config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [ $config['modules']['gii'] = [
'class' => 'yii\gii\Module', 'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1'],
'generators' => [
'crud' => [
'class' => 'yii\gii\generators\crud\Generator',
'templates' => [
'blobtCrud' => '@blobt/generators/crud/default',
]
]
],
]; ];
} }

5
kcadmin/views/category/index.php

@ -14,8 +14,7 @@ $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<?php
echo GridView::widget([
<?= GridView::widget([
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'filter' => $this->render("_search", ['model' => $searchModel]), 'filter' => $this->render("_search", ['model' => $searchModel]),
'batch' => [ 'batch' => [
@ -61,7 +60,7 @@ $this->params['breadcrumbs'][] = $this->title;
'attribute' => 'description', 'attribute' => 'description',
'enableSorting' => false, 'enableSorting' => false,
'format' => 'ntext', 'format' => 'ntext',
'width' => '55%',
'width' => '50%',
], ],
[ [
'class' => 'blobt\grid\ActionColumn', 'class' => 'blobt\grid\ActionColumn',

179
vendor/blobt/generators/crud/default/controller.php

@ -0,0 +1,179 @@
<?php
/**
* This is the template for generating a CRUD controller class file.
*/
use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
$searchModelAlias = $searchModelClass . 'Search';
}
/* @var $class ActiveRecordInterface */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();
echo "<?php\n";
?>
namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
use Yii;
use <?= ltrim($generator->modelClass, '\\') ?>;
<?php if (!empty($generator->searchModelClass)): ?>
use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
<?php else: ?>
use yii\data\ActiveDataProvider;
<?php endif; ?>
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
*/
class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all <?= $modelClass ?> models.
* @return mixed
*/
public function actionIndex()
{
<?php if (!empty($generator->searchModelClass)): ?>
$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
<?php else: ?>
$dataProvider = new ActiveDataProvider([
'query' => <?= $modelClass ?>::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
<?php endif; ?>
}
/**
* Displays a single <?= $modelClass ?> model.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView(<?= $actionParams ?>)
{
return $this->render('view', [
'model' => $this->findModel(<?= $actionParams ?>),
]);
}
/**
* Creates a new <?= $modelClass ?> model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new <?= $modelClass ?>();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', <?= $urlParams ?>]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing <?= $modelClass ?> model.
* If update is successful, the browser will be redirected to the 'view' page.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate(<?= $actionParams ?>)
{
$model = $this->findModel(<?= $actionParams ?>);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', <?= $urlParams ?>]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing <?= $modelClass ?> model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete(<?= $actionParams ?>)
{
$this->findModel(<?= $actionParams ?>)->delete();
return $this->redirect(['index']);
}
/**
* Finds the <?= $modelClass ?> model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return <?= $modelClass ?> the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(<?= $actionParams ?>)
{
<?php
if (count($pks) === 1) {
$condition = '$id';
} else {
$condition = [];
foreach ($pks as $pk) {
$condition[] = "'$pk' => \$$pk";
}
$condition = '[' . implode(', ', $condition) . ']';
}
?>
if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
return $model;
}
throw new NotFoundHttpException(<?= $generator->generateString('The requested page does not exist.') ?>);
}
}

86
vendor/blobt/generators/crud/default/search.php

@ -0,0 +1,86 @@
<?php
/**
* This is the template for generating CRUD search class of the specified model.
*/
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
$modelAlias = $modelClass . 'Model';
}
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();
echo "<?php\n";
?>
namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
/**
* <?= $searchModelClass ?> represents the model behind the search form of `<?= $generator->modelClass ?>`.
*/
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
<?= implode(",\n ", $rules) ?>,
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
<?= implode("\n ", $searchConditions) ?>
return $dataProvider;
}
}

42
vendor/blobt/generators/crud/default/views/_form.php

@ -0,0 +1,42 @@
<?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
/* @var $model \yii\db\ActiveRecord */
$model = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$safeAttributes = $model->attributes();
}
echo "<?php\n";
?>
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-form">
<?= "<?php " ?>$form = ActiveForm::begin(); ?>
<?php foreach ($generator->getColumnNames() as $attribute) {
if (in_array($attribute, $safeAttributes)) {
echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
}
} ?>
<div class="form-group">
<?= "<?= " ?>Html::submitButton(<?= $generator->generateString('Save') ?>, ['class' => 'btn btn-success']) ?>
</div>
<?= "<?php " ?>ActiveForm::end(); ?>
</div>

49
vendor/blobt/generators/crud/default/views/_search.php

@ -0,0 +1,49 @@
<?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
echo "<?php\n";
?>
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->searchModelClass, '\\') ?> */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-search">
<?= "<?php " ?>$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
<?php if ($generator->enablePjax): ?>
'options' => [
'data-pjax' => 1
],
<?php endif; ?>
]); ?>
<?php
$count = 0;
foreach ($generator->getColumnNames() as $attribute) {
if (++$count < 6) {
echo " <?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
} else {
echo " <?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
}
}
?>
<div class="form-group">
<?= "<?= " ?>Html::submitButton(<?= $generator->generateString('Search') ?>, ['class' => 'btn btn-primary']) ?>
<?= "<?= " ?>Html::resetButton(<?= $generator->generateString('Reset') ?>, ['class' => 'btn btn-outline-secondary']) ?>
</div>
<?= "<?php " ?>ActiveForm::end(); ?>
</div>

29
vendor/blobt/generators/crud/default/views/create.php

@ -0,0 +1,29 @@
<?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
echo "<?php\n";
?>
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
$this->title = <?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>;
$this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-create">
<h1><?= "<?= " ?>Html::encode($this->title) ?></h1>
<?= "<?= " ?>$this->render('_form', [
'model' => $model,
]) ?>
</div>

105
vendor/blobt/generators/crud/default/views/index.php

@ -0,0 +1,105 @@
<?php
/* blobt */
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
$urlParams = $generator->generateUrlParams();
$nameAttribute = $generator->getNameAttribute();
echo "<?php\n";
?>
use yii\helpers\Html;
use blobt\grid\GridView;
/* @var $this yii\web\View */
<?= !empty($generator->searchModelClass) ? "/* @var \$searchModel " . ltrim($generator->searchModelClass, '\\') . " */\n" : '' ?>
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
<div class="col-xs-12">
<?= "<?= " ?>
GridView::widget([
'dataProvider' => $dataProvider,
'filter' => $this->render("_search", ['model' => $searchModel]),
batch' => [
[
"label" => "删除",
"url" => "/category/deletes"
],
],
'columns' => [
],
]);
<?= "?>" ?>
</div>
</div>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index">
<h2><?= "<?= " ?>Html::encode($this->title) ?></h2>
<p>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= $generator->enablePjax ? " <?php Pjax::begin(); ?>\n" : '' ?>
<?php if (!empty($generator->searchModelClass)): ?>
<?= " <?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?>
<?php endif; ?>
<?php if ($generator->indexWidgetType === 'grid'): ?>
<?= "<?= " ?>GridView::widget([
'dataProvider' => $dataProvider,
<?= !empty($generator->searchModelClass) ? "'filterModel' => \$searchModel,\n 'columns' => [\n" : "'columns' => [\n"; ?>
['class' => 'yii\grid\SerialColumn'],
<?php
$count = 0;
if (($tableSchema = $generator->getTableSchema()) === false) {
foreach ($generator->getColumnNames() as $name) {
if (++$count < 6) {
echo " '" . $name . "',\n";
} else {
echo " //'" . $name . "',\n";
}
}
} else {
foreach ($tableSchema->columns as $column) {
$format = $generator->generateColumnFormat($column);
if (++$count < 6) {
echo " '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
} else {
echo " //'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
}
}
}
?>
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php else: ?>
<?= "<?= " ?>ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model-><?= $nameAttribute ?>), ['view', <?= $urlParams ?>]);
},
]) ?>
<?php endif; ?>
<?= $generator->enablePjax ? " <?php Pjax::end(); ?>\n" : '' ?>
</div>

40
vendor/blobt/generators/crud/default/views/update.php

@ -0,0 +1,40 @@
<?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
$urlParams = $generator->generateUrlParams();
$modelClassName = Inflector::camel2words(StringHelper::basename($generator->modelClass));
$nameAttributeTemplate = '$model->' . $generator->getNameAttribute();
$titleTemplate = $generator->generateString('Update ' . $modelClassName . ': {name}', ['name' => '{nameAttribute}']);
if ($generator->enableI18N) {
$title = strtr($titleTemplate, ['\'{nameAttribute}\'' => $nameAttributeTemplate]);
} else {
$title = strtr($titleTemplate, ['{nameAttribute}\'' => '\' . ' . $nameAttributeTemplate]);
}
echo "<?php\n";
?>
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
$this->title = <?= $title ?>;
$this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model-><?= $generator->getNameAttribute() ?>, 'url' => ['view', <?= $urlParams ?>]];
$this->params['breadcrumbs'][] = <?= $generator->generateString('Update') ?>;
?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-update">
<h1><?= '<?= ' ?>Html::encode($this->title) ?></h1>
<?= '<?= ' ?>$this->render('_form', [
'model' => $model,
]) ?>
</div>

58
vendor/blobt/generators/crud/default/views/view.php

@ -0,0 +1,58 @@
<?php
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */
$urlParams = $generator->generateUrlParams();
echo "<?php\n";
?>
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
$this->title = $model-><?= $generator->getNameAttribute() ?>;
$this->params['breadcrumbs'][] = ['label' => <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-view">
<h1><?= "<?= " ?>Html::encode($this->title) ?></h1>
<p>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Update') ?>, ['update', <?= $urlParams ?>], ['class' => 'btn btn-primary']) ?>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Delete') ?>, ['delete', <?= $urlParams ?>], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => <?= $generator->generateString('Are you sure you want to delete this item?') ?>,
'method' => 'post',
],
]) ?>
</p>
<?= "<?= " ?>DetailView::widget([
'model' => $model,
'attributes' => [
<?php
if (($tableSchema = $generator->getTableSchema()) === false) {
foreach ($generator->getColumnNames() as $name) {
echo " '" . $name . "',\n";
}
} else {
foreach ($generator->getTableSchema()->columns as $column) {
$format = $generator->generateColumnFormat($column);
echo " '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
}
}
?>
],
]) ?>
</div>
Loading…
Cancel
Save