almeyda / yii2-simple-pages

用于创建具有简单页面集合的网站的yii2扩展

安装: 7

依赖: 0

建议者: 0

安全: 0

星级: 0

观察者: 2

分支: 0

开放性问题: 0

类型:yii2-extension

dev-master 2020-12-06 15:14 UTC

This package is auto-updated.

Last update: 2024-09-06 23:32:20 UTC


README

Simple Pages 模块是 Yii2 的一个简单客户端 CMS 模块。

安装

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

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

$ php composer.phar require almeyda/yii2-simple-pages "*"

或者添加

{
    ...
    "require": {
        ...
        "almeyda/yii2-simple-pages": "*"
        ...
    }
    ...
}

到你的 composer.json 文件的 "require" 部分,并运行

$ php composer.phar update

配置

扩展安装完成后,修改你的应用程序配置以包含

return [
    ...
    'modules' => [
        ...
        'pages' => [
           'class' => 'almeyda\pages\Module',
        ],
        ...
    ],
    ...	        
    'components' => [
        ...
        'urlManager' => [
            'class' => 'codemix\localeurls\UrlManager',
            'rules' => [
                ...
                ...
                ...
                'pages' =>  [
                        'pattern' => 'blog/<view>',
                        'route' => 'pages/page/page',
                        'defaults' => ['view' => 'index'],
                        'suffix' => '.html'
                ],
                ...
            ]
        ...
        ],
        'view' => [
            ...
            'theme' => [
                'pathMap' => [
                    '@app/views/layouts' => '@app/views/themes/{your-theme}/layouts',
                    '@almeyda/pages/views/page/pages/blog' => '@app/views/themes/{your-theme}/pages',
                    ...
            ],
            ...
        ],
    ],
    ...             
]
];

使用示例

我们构建了一个非常简单的博客,具有常见的布局,在 index.php 页面上显示文章列表,以及文章(如 post1.php、post2.php 等)。我们使用基于 Yii2 路径映射功能的 'blog' 示例。请注意,规则 'pages' 应该在规则堆栈的末尾添加。否则,所有简单请求都将由 'pages' 模块处理。

###文件夹结构

views/                          (folder) basic yii2 project views folder
    └─themes/                   (folder) folder with the list of themes
        └─{your-theme}/         (folder) your theme
            ├─layout/           (folder) layouts used for blog
            │   └main.php       (file) layout html/php
            └─pages/            (folder) views used for blog 
                ├─ index.php    (file) file with the list of posts
                ├─ post1.php    (file) file with some content of the 1st post 
                └─ post2.php    (file) file with some content of the 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. 确保在配置文件组件部分使用了正确的主题 'pathMap'。
'components' => [
    ...
    'view' => [
        ...
        'theme' => [
            'pathMap' => [
                '@almeyda/pages/views/page/pages' => '@app/views/themes/{your-theme}/common/pages',
                ...
        ],
        ...
    ],
    ...
]
  1. 创建新的文件 /views/themes/{your-theme}/common/pages/faq.php;
  2. 打开地址 yourhost/faq;

主题参数

主题参数可以更改,以启用不同的视图主题。

许可证

请查看包含的 LICENSE.md 以获取详细信息。