/**
* @var frontendwidgetsEncyclopediaWidget $this->context
*/
use yiihelpersHtml;
?>
<h1><?= Html::encode($this->context->title) ?></h1>
<div class="well">
<?= $this->context->content ?>
</div>
namespace frontendwidgets;
use yiibaseWidget;
/**
* Encyclopedia
*
* @author Kalinin Alexandr
*/
class EncyclopediaWidget extends Widget
{
/**
* @var string title
*/
public $title;
/**
* @var string content
*/
public $content;
/**
* {@inheritDoc}
* @see yiibaseObject::init()
*/
public function init()
{
parent::init();
if (empty($this->title)) {
$this->title = 'Empty title';
}
if (empty($this->content)) {
$this->content = 'Empty content';
}
}
/**
* {@inheritDoc}
* @see yiibaseWidget::run()
*/
public function run()
{
return $this->render('encyclopedia');
}
}
/**
* @var frontendcontrollersEncyclopediaController $this->context
* @var commonmodelsEncyclopedia $encyclopedia
*/
use frontendwidgetsEncyclopediaWidget;
?>
<?= EncyclopediaWidget::widget([
'title' => $encyclopedia->title,
'content' => $encyclopedia->content
]) ?>
namespace frontendcontrollers;
use yiiwebNotFoundHttpException;
use commonmodelsEncyclopedia;
use yiiwebController;
/**
* Encyclopedia
*
* @author Kalinin Alexandr
*/
class EncyclopediaController extends Controller
{
/**
* @var integer cache time
*/
const CACHE_TIME = 20;
/**
* @param string $alias
* @return string
*/
public function actionDoc($alias)
{
$encyclopedia = Encyclopedia::getDb()->cache(function ($db) use ($alias) {
return Encyclopedia::getByAlias($alias);
}, self::CACHE_TIME);
if ($encyclopedia === null) {
throw new NotFoundHttpException('Not Found: 404');
}
return $this->render('doc', ['encyclopedia' => $encyclopedia]);
}
}
namespace commonmodels;
use Yii;
/**
* This is the model class for table "{{%encyclopedia}}".
*
* @property integer $id
* @property string $alias
* @property string $title
* @property string $content
* @property integer $created_at
* @property integer $updated_at
*/
class Encyclopedia extends yiidbActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%encyclopedia}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['alias', 'title', 'content', 'created_at', 'updated_at'], 'required'],
[['content'], 'string'],
[['created_at', 'updated_at'], 'integer'],
[['alias', 'title'], 'string', 'max' => 255],
[['alias'], 'unique']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'alias' => 'Alias',
'title' => 'Title',
'content' => 'Content',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* @param string $alias
* @return Encyclopedia|null
*/
public static function getByAlias($alias)
{
return static::find()->where(['alias' => $alias])->one();
}
}
use yiidbMigration;
/**
* @author Kalinin Alexandr
*/
class m160706_130958_id100_encyclopedia extends Migration
{
/**
* @var string
*/
const TABLE_NAME = '{{%encyclopedia}}';
/**
* @var string
*/
const TABLE_PARAMS = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
/**
* {@inheritDoc}
* @see yiidbMigration::up()
*/
public function up()
{
$this->createTable(self::TABLE_NAME, [
'id' => $this->primaryKey(),
'alias' => $this->string()->notNull()->unique(),
'title' => $this->string()->notNull(),
'content' => $this->text()->notNull(),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], self::TABLE_PARAMS);
}
/**
* {@inheritDoc}
* @see yiidbMigration::down()
*/
public function down()
{
$this->dropTable(self::TABLE_NAME);
}
}