sergeykoz / yii2-livecontent

为 Yii 框架轻松编辑网站内容

安装: 4

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

公开问题: 0

类型:yii2-extension

0.1.0 2019-10-02 10:29 UTC

This package is not auto-updated.

Last update: 2024-09-26 09:54:02 UTC


README

此扩展为 Yii 框架 2.0 应用程序提供了一种简单的方式来制作动态内容。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

composer require --prefer-dist sergeykoz/yii2-livecontent:0.1.0

或者在您的 composer.json 文件的 require 部分添加以下内容:

"sergeykoz/yii2-livecontent": "~0.1.0"

更新数据库模式

下载 sergeykoz/yii2-livecontent 后,您需要通过应用迁移来更新您的数据库模式

命令行

php yii migrate --migrationPath=@vendor/sergeykoz/yii2-livecontent/src/migrations

或配置 controllerMap 设置

配置

扩展安装后,只需按照以下方式修改您的应用程序配置

return [
    'bootstrap' => ['livecontent'],
    'modules' => [
        'livecontent' => [
            'class' => 'ssoft\livecontent\Module',
            //'accessRules' => [[
            //    'allow' => true,
            //    'verbs' => ['POST']
            //]],
            //'editorOptions' => [
            //    'clientOptions'=>[
            //        'rows' => 6,
            //        'autoParagraph'=>false 
            //    ],
            //    'preset' => 'full',
            //    'autoParagraph' => true
            //]
        ],
        ...
    ],
    ...
];

配置文件 console.php

return [
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => [
                'ssoft\livecontent\migrations'
            ],
        ],
    ],
];

用法

在视图

<?php

use ssoft\livecontent\Content;

...

/*
You should replace parts of content which needs to be dynamic with calling of methods:
Content::text
Content::textarea
Content::html
Content::block

This mehtods need two arguments except of Content::block.

place -  uses for identification of the content place. You have to specify a unique key for current view file. You can insert language prefix for multilanguage applications.

isAllow - a flag which allow to show edit element on the page. You can use different cases for the argument. For basic application a case which allow to change content only for logged user is !Yii::$app->user->isGuest. For applications based on RBAC access you can use \Yii::$app->user->can('admin')

blockLogic - the argument uses just for Content::block method which consists HTML template and fields to the template.
*/


echo Content::text('text-id-'.\Yii::$app->language, !Yii::$app->user->isGuest);

...

echo Content::textarea('textblock-id', !Yii::$app->user->isGuest); // allowed to logged user

...

echo Content::html('formatted-text-id', \Yii::$app->user->can('admin')); // allowed for RBAC role  admin

...

$block=[
    'template' => "<div class=\"col-lg-4\">{edit}
        <h2>{head}</h2>
        {description}
        {show_link on}<p><a class=\"btn {button}\" href=\"{link is null}#{/link is null}{link is not null}{link}{/link is not null}\">{caption}</a></p>{/show_link on}
    </div>",               
    'rules'=> [
        'edit'=>['type'=>'editcontrol',],              
        'head' => ['name' => 'Title', 'type' => 'text', 'required' => true],
        'description' => ['name' => 'Description', 'type' => 'html', 'required' => true],
        'caption' => ['name' => 'Text', 'type'=>'text', 'required' => true],
        'link' => ['name' => 'Link','type'=>'text'],
        'show_link' => ['name' => 'Show a button','type'=>'checkbox', 'required' => true],
        'button' => [
            'name' => 'Button type',
            'type' => 'select',
            'options'=>[
                ['option'=>'Default', 'value' => 'btn-default'],
                ['option'=>'Primary', 'value' => 'btn-primary'],
                ['option'=>'Success', 'value' => 'btn-success'],
                ['option'=>'Info', 'value' => 'btn-info'],
                ['option'=>'Warning', 'value' => 'btn-warning'],
                ['option'=>'Danger', 'value' => 'btn-danger'],
            ],
            'required' => true
        ],
    ]
];
echo Content::block('block-id', !Yii::$app->user->isGuest, $block);  
?>