diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..69fce2e --- /dev/null +++ b/readme.md @@ -0,0 +1,87 @@ +Yii2 page module +=================== + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist omny/yii2-page-module "*" +``` + +or add + +``` +"omny/yii2-page-module": "*" +``` + +for actual code and fixes: + +``` +"omny/yii2-page-module": "dev-master" +``` + +to the require section of your `composer.json` file. + +Migrate +--- + +The last thing you need to do is updating your database schema by applying the migrations. Make sure that you have properly configured db application component and run the following command: + +``` +$ php yii migrate/up --migrationPath=@vendor/omny/yii2-page-module/src/migrations +``` + +Using +--- + +Create controller and extend it from backend or frontend one: + +``` +use omny\yii2\page\module\controllers\BackendController; + +class PageController extends BackendController +{ + +} +``` + +or using by actions: + +``` + [ + 'class' => CreateAction::class + ], + 'manage' => [ + 'class' => ManageAction::class, + ], + 'update' => [ + 'class' => UpdateAction::class, + ] + ]; + } +} +``` diff --git a/src/actions/CreateAction.php b/src/actions/CreateAction.php new file mode 100644 index 0000000..7509785 --- /dev/null +++ b/src/actions/CreateAction.php @@ -0,0 +1,35 @@ + \Yii::$app->getUser()->getId() + ]); + + if ($model->load(\Yii::$app->getRequest()->post()) && $model->save()) { + return $this->controller->redirect([$this->redirectUrl]); + } + + return $this->controller->render($this->viewFile, ['model' => $model]); + } +} diff --git a/src/actions/ManageAction.php b/src/actions/ManageAction.php new file mode 100644 index 0000000..81c7ed5 --- /dev/null +++ b/src/actions/ManageAction.php @@ -0,0 +1,32 @@ + Page::find(), + ]); + $viewUrl = $this->viewUrl; + + return $this->controller->render($this->viewFile, compact('provider', 'viewUrl')); + } +} diff --git a/src/actions/UpdateAction.php b/src/actions/UpdateAction.php new file mode 100644 index 0000000..1b132e2 --- /dev/null +++ b/src/actions/UpdateAction.php @@ -0,0 +1,42 @@ +get(PageRepository::class); + $model = $repository->getById($id); + + if ($model->load(\Yii::$app->getRequest()->post()) && $model->save()) { + return $this->controller->redirect([$this->redirectUrl]); + } + + return $this->controller->render($this->viewFile, ['model' => $model]); + } +} diff --git a/src/actions/ViewAction.php b/src/actions/ViewAction.php new file mode 100644 index 0000000..e4df369 --- /dev/null +++ b/src/actions/ViewAction.php @@ -0,0 +1,35 @@ +get(PageRepository::class); + $model = $repository->getBySlug($slug); + + return $this->controller->render($this->viewFile, ['model' => $model]); + } +} diff --git a/src/controllers/BackendController.php b/src/controllers/BackendController.php new file mode 100644 index 0000000..7014fb2 --- /dev/null +++ b/src/controllers/BackendController.php @@ -0,0 +1,33 @@ + [ + 'class' => CreateAction::class, + ], + 'manage' => [ + 'class' => ManageAction::class, + ], + 'update' => [ + 'class' => UpdateAction::class, + ] + ]; + } +} diff --git a/src/controllers/FrontendController.php b/src/controllers/FrontendController.php new file mode 100644 index 0000000..db2acfc --- /dev/null +++ b/src/controllers/FrontendController.php @@ -0,0 +1,25 @@ + [ + 'class' => ViewAction::class + ] + ]; + } +} diff --git a/src/migrations/m180000_000000_page.php b/src/migrations/m180000_000000_page.php new file mode 100644 index 0000000..d2f9806 --- /dev/null +++ b/src/migrations/m180000_000000_page.php @@ -0,0 +1,53 @@ +createTable( + Page::tableName(), + [ + 'id' => $this->bigPrimaryKey()->unsigned(), + 'title' => $this->string()->notNull(), + 'slug' => $this->string()->unique()->notNull(), + 'body' => $this->text(), + 'user_id' => $this->bigInteger()->unsigned()->null()->defaultValue(null), + 'created' => $this->timestamp()->defaultExpression(self::CURRENT_TIMESTAMP_EXPRESSION), + 'updated' => $this->timestamp()->defaultExpression(self::DEFAULT_ON_UPDATE_EXPRESSION), + ] + ); + $this->createIndex( + sprintf(self::INDEX_TEMPLATE, Page::tableName(), 'slug'), + Page::tableName(), + 'slug', + true + ); + $this->createIndex( + sprintf(self::INDEX_TEMPLATE, Page::tableName(), 'user_id'), + Page::tableName(), + 'user_id' + ); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable(Page::tableName()); + } +} diff --git a/src/models/Page.php b/src/models/Page.php new file mode 100644 index 0000000..19afad4 --- /dev/null +++ b/src/models/Page.php @@ -0,0 +1,55 @@ + SluggableBehavior::class, + 'attribute' => 'title', + 'immutable' => true, + ]; + + return $behaviors; + } +} diff --git a/src/repositories/PageRepository.php b/src/repositories/PageRepository.php new file mode 100644 index 0000000..ad93c1d --- /dev/null +++ b/src/repositories/PageRepository.php @@ -0,0 +1,46 @@ + $id]); + + if ($model !== null) { + return $model; + } + + throw new NotFoundHttpException('Page not found.'); + } + + /** + * @param string $slug + * @return Page + * @throws NotFoundHttpException + */ + public function getBySlug(string $slug): Page + { + $model = Page::findOne(['slug' => $slug]); + + if ($model !== null) { + return $model; + } + + throw new NotFoundHttpException('Page not found.'); + } +} diff --git a/src/views/_page-form.php b/src/views/_page-form.php new file mode 100644 index 0000000..3443a79 --- /dev/null +++ b/src/views/_page-form.php @@ -0,0 +1,21 @@ +field($model, 'title')->textInput(); +echo $form->field($model, 'body')->textarea(); + +echo Html::submitButton('Save'); + +ActiveForm::end(); \ No newline at end of file diff --git a/src/views/create.php b/src/views/create.php new file mode 100644 index 0000000..63b0c67 --- /dev/null +++ b/src/views/create.php @@ -0,0 +1,13 @@ +title = 'Create page'; + +echo $this->render('_page-form', ['model' => $model]); diff --git a/src/views/manage.php b/src/views/manage.php new file mode 100644 index 0000000..cd2d269 --- /dev/null +++ b/src/views/manage.php @@ -0,0 +1,50 @@ +title = 'Page manage'; + +echo Html::a('Create', ['create'], ['class' => 'btn btn-sm btn-success']); +echo Html::tag('br'); +echo Html::tag('br'); + +echo GridView::widget([ + 'dataProvider' => $provider, + 'columns' => [ + 'id', + [ + 'attribute' => 'title', + 'content' => function (Page $model) use ($viewUrl) { + return Html::a($model->title, [$viewUrl, 'id' => $model->id]); + } + ], + 'slug', + 'body', + 'user_id', + 'created', + 'updated', + + [ + 'class' => \yii\grid\ActionColumn::class, + 'template' => '{update} {delete}', + 'buttons' => [ + 'update' => function ($url, $model, $key) { + return Html::a('Update', $url, ['class' => 'btn btn-sm btn-success']); + }, + 'delete' => function ($url, $model, $key) { + return Html::a('Delete', $url, ['class' => 'ml-2 btn btn-sm btn-danger']); + } + ], + ] + ] +]); diff --git a/src/views/update.php b/src/views/update.php new file mode 100644 index 0000000..8afca16 --- /dev/null +++ b/src/views/update.php @@ -0,0 +1,13 @@ +title = sprintf('Update «%s»', $model->title); + +echo $this->render('_page-form', ['model' => $model]); diff --git a/src/views/view.php b/src/views/view.php new file mode 100644 index 0000000..58d727a --- /dev/null +++ b/src/views/view.php @@ -0,0 +1,13 @@ +title = $model->title; + +echo $model->body; \ No newline at end of file