nwxuiw
Last Updated: August 11, 2016
·
154
· kalinin84

How to create a simple encyclopedia in PHP?

/**
 * @var frontend\widgets\EncyclopediaWidget $this->context
 */

use yii\helpers\Html;
?>

<h1><?= Html::encode($this->context->title) ?></h1>

<div class="well">
    <?= $this->context->content ?>
</div>
namespace frontend\widgets;

use yii\base\Widget;

/**
 * Encyclopedia
 * 
 * @author Kalinin Alexandr
 */
class EncyclopediaWidget extends Widget
{
    /**
     * @var string title
     */
    public $title;

    /**
     * @var string content
     */
    public $content;

    /**
     * {@inheritDoc}
     * @see \yii\base\Object::init()
     */
    public function init()
    {
        parent::init();

        if (empty($this->title)) {
            $this->title = 'Empty title';
        }

        if (empty($this->content)) {
            $this->content = 'Empty content';
        }
    }

    /**
     * {@inheritDoc}
     * @see \yii\base\Widget::run()
     */
    public function run()
    {
        return $this->render('encyclopedia');
    }
}
/**
 * @var frontend\controllers\EncyclopediaController $this->context
 * @var common\models\Encyclopedia $encyclopedia
 */

use frontend\widgets\EncyclopediaWidget;
?>

<?= EncyclopediaWidget::widget([
    'title' => $encyclopedia->title, 
    'content' => $encyclopedia->content
]) ?>
namespace frontend\controllers;

use yii\web\NotFoundHttpException;
use common\models\Encyclopedia;
use yii\web\Controller;

/**
 * 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 common\models;

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 \yii\db\ActiveRecord
{
    /**
     * @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 yii\db\Migration;

/**
 * @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 \yii\db\Migration::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 \yii\db\Migration::down()
     */
    public function down()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}