Skip to content

Commit

Permalink
simple version
Browse files Browse the repository at this point in the history
  • Loading branch information
hello-omny committed Jul 30, 2019
1 parent f345f23 commit 4c20f96
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 0 deletions.
87 changes: 87 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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:

```
<?php
namespace app\controllers\backend;
use omny\yii2\page\module\actions\CreateAction;
use omny\yii2\page\module\actions\ManageAction;
use omny\yii2\page\module\actions\UpdateAction;
/**
* Class PageController
* @package app\controllers\backend
*/
class PageController extends AbstractController
{
/**
* @return array
*/
public function actions()
{
return [
'create' => [
'class' => CreateAction::class
],
'manage' => [
'class' => ManageAction::class,
],
'update' => [
'class' => UpdateAction::class,
]
];
}
}
```
35 changes: 35 additions & 0 deletions src/actions/CreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace omny\yii2\page\module\actions;

use omny\yii2\page\module\models\Page;
use yii\base\Action;
use yii\web\Response;

/**
* Class CreateAction
* @package omny\yii2\page\module\actions
*/
class CreateAction extends Action
{
/** @var string */
public $viewFile = '@vendor/omny/yii2-page-module/src/views/create.php';
/** @var string */
public $redirectUrl = 'manage';

/**
* @return string|Response
*/
public function run()
{
$model = new Page([
'user_id' => \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]);
}
}
32 changes: 32 additions & 0 deletions src/actions/ManageAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace omny\yii2\page\module\actions;

use omny\yii2\page\module\models\Page;
use yii\base\Action;
use yii\data\ActiveDataProvider;

/**
* Class ManageAction
* @package omny\yii2\page\module\actions
*/
class ManageAction extends Action
{
/** @var string */
public $viewFile = '@vendor/omny/yii2-page-module/src/views/manage.php';
/** @var string */
public $viewUrl = 'view';

/**
* @return string
*/
public function run(): string
{
$provider = new ActiveDataProvider([
'query' => Page::find(),
]);
$viewUrl = $this->viewUrl;

return $this->controller->render($this->viewFile, compact('provider', 'viewUrl'));
}
}
42 changes: 42 additions & 0 deletions src/actions/UpdateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace omny\yii2\page\module\actions;

use omny\yii2\page\module\repositories\PageRepository;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\di\NotInstantiableException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
* Class UpdateAction
* @package omny\yii2\page\module\actions
*/
class UpdateAction extends Action
{
/** @var string */
public $viewFile = '@vendor/omny/yii2-page-module/src/views/update.php';
/** @var string */
public $redirectUrl = 'manage';

/**
* @param int $id
* @return string|Response
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws NotInstantiableException
*/
public function run(int $id)
{
/** @var PageRepository $repository */
$repository = \Yii::$container->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]);
}
}
35 changes: 35 additions & 0 deletions src/actions/ViewAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace omny\yii2\page\module\actions;

use omny\yii2\page\module\repositories\PageRepository;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\di\NotInstantiableException;
use yii\web\NotFoundHttpException;

/**
* Class ViewAction
* @package omny\yii2\page\module\actions
*/
class ViewAction extends Action
{
/** @var string */
public $viewFile = '@vendor/omny/yii2-page-module/src/views/view.php';

/**
* @param string $slug
* @return string
* @throws NotFoundHttpException
* @throws InvalidConfigException
* @throws NotInstantiableException
*/
public function run(string $slug): string
{
/** @var PageRepository $repository */
$repository = \Yii::$container->get(PageRepository::class);
$model = $repository->getBySlug($slug);

return $this->controller->render($this->viewFile, ['model' => $model]);
}
}
33 changes: 33 additions & 0 deletions src/controllers/BackendController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace omny\yii2\page\module\controllers;

use omny\yii2\page\module\actions\CreateAction;
use omny\yii2\page\module\actions\ManageAction;
use omny\yii2\page\module\actions\UpdateAction;
use yii\web\Controller;

/**
* Class DefaultController
* @package omny\yii2\page\module\controllers\backend
*/
class BackendController extends Controller
{
/**
* @return array
*/
public function actions(): array
{
return [
'create' => [
'class' => CreateAction::class,
],
'manage' => [
'class' => ManageAction::class,
],
'update' => [
'class' => UpdateAction::class,
]
];
}
}
25 changes: 25 additions & 0 deletions src/controllers/FrontendController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace omny\yii2\page\module\controllers;

use omny\yii2\page\module\actions\ViewAction;
use yii\web\Controller;

/**
* Class DefaultController
* @package omny\yii2\page\module\controllers\frontend
*/
class FrontendController extends Controller
{
/**
* @return array
*/
public function actions()
{
return [
'view' => [
'class' => ViewAction::class
]
];
}
}
53 changes: 53 additions & 0 deletions src/migrations/m180000_000000_page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use omny\yii2\page\module\models\Page;
use yii\db\Migration;

/**
* Class m180000_000000_page
*/
class m180000_000000_page extends Migration
{
const INDEX_TEMPLATE = 'idx__%s__%s';

const CURRENT_TIMESTAMP_EXPRESSION = 'current_timestamp()';
const DEFAULT_ON_UPDATE_EXPRESSION = "'0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP";

/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->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());
}
}
Loading

0 comments on commit 4c20f96

Please sign in to comment.