Commit 4548d620 by dianc

Category CRUD

parent 17a7367f
<?php <?php
$config = [ $config = [
'homeUrl'=>Yii::getAlias('@backendUrl'), 'homeUrl' => Yii::getAlias('@backendUrl'),
'controllerNamespace' => 'backend\controllers', 'controllerNamespace' => 'backend\controllers',
'defaultRoute'=>'timeline-event/index', 'defaultRoute' => 'timeline-event/index',
'controllerMap'=>[ 'controllerMap' => [
'file-manager-elfinder' => [ 'file-manager-elfinder' => [
'class' => 'mihaildev\elfinder\Controller', 'class' => 'mihaildev\elfinder\Controller',
'access' => ['manager'], 'access' => ['manager'],
...@@ -12,13 +12,13 @@ $config = [ ...@@ -12,13 +12,13 @@ $config = [
[ [
'baseUrl' => '@storageUrl', 'baseUrl' => '@storageUrl',
'basePath' => '@storage', 'basePath' => '@storage',
'path' => '/', 'path' => '/',
'access' => ['read' => 'manager', 'write' => 'manager'] 'access' => ['read' => 'manager', 'write' => 'manager']
] ]
] ]
] ]
], ],
'components'=>[ 'components' => [
'errorHandler' => [ 'errorHandler' => [
'errorAction' => 'site/error', 'errorAction' => 'site/error',
], ],
...@@ -26,52 +26,55 @@ $config = [ ...@@ -26,52 +26,55 @@ $config = [
'cookieValidationKey' => env('BACKEND_COOKIE_VALIDATION_KEY') 'cookieValidationKey' => env('BACKEND_COOKIE_VALIDATION_KEY')
], ],
'user' => [ 'user' => [
'class'=>'yii\web\User', 'class' => 'yii\web\User',
'identityClass' => 'common\models\User', 'identityClass' => 'common\models\User',
'loginUrl'=>['sign-in/login'], 'loginUrl' => ['sign-in/login'],
'enableAutoLogin' => true, 'enableAutoLogin' => true,
'as afterLogin' => 'common\behaviors\LoginTimestampBehavior' 'as afterLogin' => 'common\behaviors\LoginTimestampBehavior'
], ],
], ],
'modules'=>[ 'modules' => [
'i18n' => [ 'i18n' => [
'class' => 'backend\modules\i18n\Module', 'class' => 'backend\modules\i18n\Module',
'defaultRoute'=>'i18n-message/index' 'defaultRoute' => 'i18n-message/index'
],
'theme' => [
'class' => 'backend\modules\theme\Module',
] ]
], ],
'as globalAccess'=>[ 'as globalAccess' => [
'class'=>'\common\behaviors\GlobalAccessBehavior', 'class' => '\common\behaviors\GlobalAccessBehavior',
'rules'=>[ 'rules' => [
[ [
'controllers'=>['sign-in'], 'controllers' => ['sign-in'],
'allow' => true, 'allow' => true,
'roles' => ['?'], 'roles' => ['?'],
'actions'=>['login'] 'actions' => ['login']
], ],
[ [
'controllers'=>['sign-in'], 'controllers' => ['sign-in'],
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
'actions'=>['logout'] 'actions' => ['logout']
], ],
[ [
'controllers'=>['site'], 'controllers' => ['site'],
'allow' => true, 'allow' => true,
'roles' => ['?', '@'], 'roles' => ['?', '@'],
'actions'=>['error'] 'actions' => ['error']
], ],
[ [
'controllers'=>['debug/default'], 'controllers' => ['debug/default'],
'allow' => true, 'allow' => true,
'roles' => ['?'], 'roles' => ['?'],
], ],
[ [
'controllers'=>['user'], 'controllers' => ['user'],
'allow' => true, 'allow' => true,
'roles' => ['administrator'], 'roles' => ['administrator'],
], ],
[ [
'controllers'=>['user'], 'controllers' => ['user'],
'allow' => false, 'allow' => false,
], ],
[ [
...@@ -84,11 +87,11 @@ $config = [ ...@@ -84,11 +87,11 @@ $config = [
if (YII_ENV_DEV) { if (YII_ENV_DEV) {
$config['modules']['gii'] = [ $config['modules']['gii'] = [
'class'=>'yii\gii\Module', 'class' => 'yii\gii\Module',
'generators' => [ 'generators' => [
'crud' => [ 'crud' => [
'class'=>'yii\gii\generators\crud\Generator', 'class' => 'yii\gii\generators\crud\Generator',
'templates'=>[ 'templates' => [
'yii2-starter-kit' => Yii::getAlias('@backend/views/_gii/templates') 'yii2-starter-kit' => Yii::getAlias('@backend/views/_gii/templates')
], ],
'template' => 'yii2-starter-kit', 'template' => 'yii2-starter-kit',
......
<?php
namespace backend\modules\theme\controllers;
use Yii;
use common\models\Category;
use backend\modules\theme\models\search\CategorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CategoryController implements the CRUD actions for Category model.
*/
class CategoryController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Category models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new CategorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Category model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Category model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Category();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Category model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Category model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Category model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Category the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Category::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
<?php
namespace backend\modules\theme\models\search;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Category;
/**
* CategorySearch represents the model behind the search form about `common\models\Category`.
*/
class CategorySearch extends Category
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'status', 'created_at', 'updated_at'], 'integer'],
[['name', 'slug', 'description', 'image_url', 'image_path'], 'safe'],
];
}
/**
* @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 = Category::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'slug', $this->slug])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'image_url', $this->image_url])
->andFilterWhere(['like', 'image_path', $this->image_path]);
return $dataProvider;
}
}
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
/* @var $form yii\bootstrap\ActiveForm */
?>
<div class="category-form">
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'slug')->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'description')->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'image_url')->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'image_path')->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'status')->textInput() ?>
<?php echo $form->field($model, 'created_at')->textInput() ?>
<?php echo $form->field($model, 'updated_at')->textInput() ?>
<div class="form-group">
<?php echo Html::submitButton($model->isNewRecord ? Yii::t('backend', 'Create') : Yii::t('backend', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\theme\models\search\CategorySearch */
/* @var $form yii\bootstrap\ActiveForm */
?>
<div class="category-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?php echo $form->field($model, 'id') ?>
<?php echo $form->field($model, 'name') ?>
<?php echo $form->field($model, 'slug') ?>
<?php echo $form->field($model, 'description') ?>
<?php echo $form->field($model, 'image_url') ?>
<?php // echo $form->field($model, 'image_path') ?>
<?php // echo $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?php echo Html::submitButton(Yii::t('backend', 'Search'), ['class' => 'btn btn-primary']) ?>
<?php echo Html::resetButton(Yii::t('backend', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->title = Yii::t('backend', 'Create {modelClass}', [
'modelClass' => 'Category',
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-create">
<?php echo $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\theme\models\search\CategorySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('backend', 'Categories');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?php echo Html::a(Yii::t('backend', 'Create {modelClass}', [
'modelClass' => 'Category',
]), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'slug',
'description',
'image_url:url',
// 'image_path',
// 'status',
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->title = Yii::t('backend', 'Update {modelClass}: ', [
'modelClass' => 'Category',
]) . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('backend', 'Update');
?>
<div class="category-update">
<?php echo $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-view">
<p>
<?php echo Html::a(Yii::t('backend', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?php echo Html::a(Yii::t('backend', 'Delete'), ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('backend', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<?php echo DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name',
'slug',
'description',
'image_url:url',
'image_path',
'status',
'created_at',
'updated_at',
],
]) ?>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment