2017 © Pedro Peláez
 

yii2-extension yii2-cms

CMS for Yii2

image

nullref/yii2-cms

CMS for Yii2

  • Thursday, July 19, 2018
  • by ZAYEC77
  • Repository
  • 8 Watchers
  • 5 Stars
  • 1,456 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 7 Open issues
  • 7 Versions
  • 4 % Grown

The README.md

Content Management Module for Yii2

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist nullref/yii2-cms "*"

or add, (*4)

"nullref/yii2-cms": "*"

to the require section of your composer.json file., (*5)

Run command php yii module/install nullref/yii2-cms to install this module. It will be added to your application config (@app/config/installed_modules.php), (*6)

Concept

This module allows you to build dynamic pages which consist of blocks (widget with config). You can create custom widgets and register it in BlockManager., (*7)

Also you can create pages with html content by WYSIWYG CKEditor., (*8)

User Guide

BlockManager

This component contains information about available blocks. You can override it:, (*9)

    'cms' => [
        'class' => 'nullref\\cms\\Module',
        'components' => [
            'blockManager' => 'app\components\BlockManager',
        ]
    ],

and add in your class own blocks:, (*10)

class BlockManager extends BaseBlockManager
{
    public function getList()
    {
        return array_merge([
            'smile' => 'app\blocks\smile', //namespace of block files
        ], parent::getList());
    }
}

To register block at runtime:, (*11)

    Block::getManager()->register('smile','app\blocks\smile');
    //or
    Yii::$app->getModule($moduleId)->get('blockManager')->register('smile','app\blocks\smile');

LinkManager

This component is used for unified access to links of different resources (e.g. page or categories). Manager can generate link to resource by it type and id. If you want to add own type of links you need to add link provider to this manager by definition container (DI). For example:, (*12)

Yii::$container->set(LinkManager::className(), [
    'class' => LinkManager::className(),
    'providers' => [
        'page' => [
            'class' => 'app\modules\cms\components\PageLinkProvider',
        ],
],]);

Each link provider must to impelement LinkProvider interface., (*13)

As result you can use this manager to generate link in your widgets or other application parts. E.g:, (*14)

/** in some component constructor define additional parameter and set it in class property **/
public function __construct(LinkManager $linkManager, $config = [])
{
    $this->linkManager = $linkManager;
    parent::__construct($config);
}
/** generate link **/
echo $this->linkManager->createUrl('page', $id);

Block structure convention

A valid block is represented by a folder with two classes:, (*15)

  • Block - define data block to use
  • Widget - run with data when this block use on page

In most cases form file will also be in this folder, (*16)

When you add own block you have to set unique id and namespace of block files folder., (*17)

Single block usage

You can use cms blocks on you own views, to call by id:, (*18)

use nullref\cms\components\Block;
?>
<div class="site-index">
    <?= Block::getBlock('hello')->run() ?>
    or
    <?= Block::getBlock('hello2') //block has override method __toString() ?>
</div>

Pages

This module allows to create pages with custom content and set custom url for it. By default all pages are available by route /pages/<route>, but you can override it by config:, (*19)

/** module config **/
'cms' => [
    'class' => 'nullref\cms\Module',
    'urlPrefix' => '', //make empty prefix
],

By default you can set meta tags for each page. Default tags are:, (*20)

  • title
  • description
  • keywords
  • robots

You can override method getMetaTypesList in Page model class (see below) to extend it., (*21)

Overriding

  • Models and ActiveQueries:
/** module config **/
'cms' => [
    'classMap' => [
        'Block' => 'app\models\cms\Block',
        'BlockQuery' => 'app\models\cms\BlockQuery',
        'Page' => 'app\models\cms\Page',
        'PageHasBlock' => 'app\models\cms\PageHasBlock',
        'PageQuery' => 'app\models\cms\PageQuery',
    ],
],
[
 /** App config **/
 'components' => [
  'i18n' => [
      'translations' => [
          '*' => ['class' => 'yii\i18n\PhpMessageSource'],
          'cms' => ['class' => 'nullref\core\components\i18n\PhpMessageSource'],
      ],
  ],
 ]
]
/** App config **/
'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@nullref/cms/views' => '@app/views/cms'
            ],
        ],
    ],
],
/** module config **/
'cms' => [
    'class' => 'nullref\cms\Module',
    'controllerNamespace' => 'app\modules\cms\controllers',
    'controllerMap' => [
        // declares "page" controller using a class name
        'page' => 'app\controllers\PageController',
    ],
],

Admin Panel

You can use this module with Yii2 Admin module., (*22)

The Versions