Skip to content

NullRefExcep/yii2-cms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Content Management Module for Yii2

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"nullref/yii2-cms": "*"

to the require section of your composer.json file.

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)

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.

Also you can create pages with html content by WYSIWYG CKEditor.

BlockManager

This component contains information about available blocks. You can override it:

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

and add in your class own blocks:

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:

    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:

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

Each link provider must to impelement LinkProvider interface.

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

/** 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:

  • 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

When you add own block you have to set unique id and namespace of block files folder.

Single block usage

You can use cms blocks on you own views, to call by id:

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:

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

By default you can set meta tags for each page. Default tags are:

  • title
  • description
  • keywords
  • robots

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

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.