almeyda/yii2-emcms

基本的yii2内容管理扩展,用于创建简单的网站

安装: 8

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

dev-master 2020-11-26 12:30 UTC

This package is auto-updated.

Last update: 2024-09-05 17:11:47 UTC


README

Emcms 是一个简单的 Yii2 客户端内容管理模块。

安装

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

composer.phar 文件放在与 composer.json 文件相同的目录中,然后运行

$ php composer.phar require almeyda/yii2-emcms "dev-master"

或添加

{
    ...
    "require": {
        ...
        "almeyda/yii2-emcms": "dev-master"
        ...
    }
    ...
}

到您的 composer.json 文件的 "require" 部分,然后运行

$ php composer.phar update

更新数据库模式

您需要做的最后一件事是通过应用迁移来更新您的数据库模式。请确保您已正确配置 db 应用程序组件,并运行以下命令

$ php yii migrate/up --migrationPath=@vendor/almeyda/yii2-emcms/migrations

配置

一旦安装了扩展,请修改您的应用程序配置以包括

return [
    ...
    'modules' => [
        ...
        'emcms' => [
           'class' => 'almeyda\emcms\Module',
        ],
        ...
    ],
    ...	        
    'components' => [
        ...
        'urlManager' => [
            'class' => 'codemix\localeurls\UrlManager',
            'rules' => [
                ...
                ...
                ...
                'emcms' =>  [
                        'pattern' => '<view>',
                        'route' => 'emcms/page/page',
                        'defaults' => ['view' => 'index'],
                ],
                ...
            ]
        ...
        ],
    ],
    ...             
]
];

请注意,规则 'emcms' 应该添加到规则堆栈的末尾。否则,所有简单请求都将由 emcms 模块处理。

覆盖视图

当您开始使用 yii2-emcms 时,您可能会发现您需要覆盖模块提供的默认视图。虽然视图名称不可配置,但 Yii2 提供了一种使用主题覆盖视图的方法。要开始,您应按如下方式配置您的视图应用程序组件

...
'components' => [
    'view' => [
        ...
        'theme' => [
            'pathMap' => [
                '@almeyda/emcms/views/layouts' => '@app/views/emcms/layouts',
                '@almeyda/emcms/views' => '@app/views/emcms/pages',
                ...
        ],
        ...
    ],
],
...

在上述 pathMap 中,表示将首先在 @app/views/emcms/pages 下搜索 @almeyda/emcms/views 中的每个视图,如果主题目录中存在视图,则将使用该视图而不是原始视图。

用法

在您的主题文件夹中创建 index.php 页面,并将您的页面添加到该文件夹(如 post1.php、post2.php 等)

##文件夹结构的示例

views/                        (folder) main folder in the root of your site
    └─emcms/                  (folder) - folder containing all cms related views/layouts
        ├─layout/             (folder) - folder containing layout 
            └main.php         (file) - layout name. It matches 'pathMap' in the configuration        
        └─pages/              (folder) - folder containing all the views. It matches 'pathMap' in the configuration 
            ├─ index.php      (file) - file with the list of posts
            ├─ post1.php      (file) - file with the content of 1st post 
            └─ post2.php      (file) - file with the content of 2nd post

文件示例

[main.php] 使用的布局

<?php
use yii\helpers\Html;
use yii\bootstrap\BootstrapAsset;
BootstrapAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>

<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

[index.php] 包含业务逻辑的文件,显示博客中的帖子列表

<?php $this->title = 'Title of a blog page' ?>

<?php $this->registerMetaTag(['name' => 'description', 'content' => 'some useful content for SEO']); ?>

<?php

# next step we form the list of files in the current directory

$posts = [];
chdir(__DIR__);
array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files); 


# next step we prepare an array to use it in special function **

foreach ($files as $fileName) {
    if (!in_array($fileName, ['.', '..', 'index.php', 'contact.php'])) {
        $fileContent = file_get_contents(__DIR__ . '/' . $fileName, true);

        foreach (['title', 'description'] as $id) {
            preg_match('#<(.*?)id=\"' . $id . '\"[^>]*>(.*?)</(.&?)>#i', $fileContent, $matches);
            $post['url'] = '/blog/' . substr($fileName, 0, -4) . '.html';
            $post[$id] = strip_tags(@$matches[2]);
        }
        if ($post['title'] || $post['description']) {
            $posts[] = $post;
        }
    }
}

# next step we form the list of files in the current directory

$provider = new \yii\data\ArrayDataProvider([
    'allModels' => $posts,
    'pagination' => [
        'pageSize' => 10,
    ],
    'sort' => [
        'attributes' => ['title', 'description'],
    ],
]);

# next step we show the list of posts in the same html-blocks

foreach ($provider->getModels() as $post) : ?>
    <div class="row">
        <p><a HREF="<?= $post['url'] ?>" class="post-heading"><?= $post['title'] ?></a></p>
            <p><?= $post['description'] ?></p>
            <a HREF="<?= $post['url'] ?>" target="_self">Read More</a>
    </div>
<?php endforeach; ?>

# next step we show the pagination if nessesary

<div class="text-center">
    <?= \yii\widgets\LinkPager::widget(['pagination' => $provider->getPagination(),]);?>
</div>

[post1.php] 包含第一篇帖子内容的示例文件

<?php $this->title = 'Title of the 1st blog post' ?>
<?php $this->registerMetaTag(['name' =>'description','content' =>'some useful content for SEO optimization' ]); ?>
<div class="row">
    <a HREF="yourhost/blog/" target="_self" class="btn btn-default">Back to blog</a>
</div>
<div class="row">
    <p>html-content of 1st blog page</p>
</div>

[post2.php] 包含第二篇帖子内容的示例文件

<?php $this->title = 'Title of the 2nd blog post' ?>
<?php $this->registerMetaTag(['name' =>'description','content' =>'some useful content for SEO optimization' ]); ?>
<div class="row">
    <a HREF="yourhost/blog/" target="_self" class="btn btn-default">Back to blog</a>
</div>
<div class="row">
    <p>html-content of 2nd blog page</p>
</div>

URL 示例

  • 首页: yourhost
  • 博客页面: yourhost/blog
  • 第一篇帖子的博客页面: yourhost/blog/post1.html
  • 第二篇帖子的博客页面: yourhost/blog/post2.html

创建单独新页面的示例

如果您想创建地址为 yourhost/faq 的页面,您可以按照以下指南进行操作

  1. 在配置文件中更改组件部分
'components' => [
    ...
    'view' => [
        ...
        'theme' => [
            'pathMap' => [
                '@app/views/layouts' => '@app/views/themes/{your-theme}/common/layouts',
                '@almeyda/emcms/views/page/pages' => '@app/views/themes/{your-theme}/common/pages',
                ...
        ],
        ...
    ],
    ...
]
  1. 创建新的文件 /views/themes/{your-theme}/common/pages/faq.php;
  2. 打开地址 yourhost/faq;

身份验证支持

阅读身份验证支持文档

许可证

请参阅附带 LICENSE.md 了解详细信息。