sy/bootstrap-article

用于添加文章功能的插件

1.4.2 2024-06-07 07:58 UTC

This package is auto-updated.

Last update: 2024-09-07 08:33:10 UTC


README

sy/bootstrap 插件,用于在基于 sy/project 的应用中添加 "文章" 功能。

安装

从你的 sy/project 基于的应用程序目录中,运行以下命令

composer install-plugin article

注意事项

安装插件命令将执行以下所有步骤

1. 安装包 sy/bootstrap-article

composer require sy/bootstrap-article

2. 将数据库安装文件复制到您的 sql 目录

使用数据库安装脚本:sql/install.sql

3. 复制模板文件

将模板文件复制到您的项目模板目录:protected/templates/Application/content

4. 复制语言文件

将语言文件夹 lang/bootstrap-article 复制到您的项目语言目录:protected/lang

5. 复制 SCSS 文件

将 scss 文件 scss/_bootstrap-article.scss 复制到您的项目 scss 目录:protected/scss

在您的 app.scss 文件中导入它并重新构建 css 文件。

6. 复制资产文件

7. 运行 composer build

8. 运行 composer db migrate

页面方法

在您的 Project\Application\Page 类中创建 2 个方法(在 protected/src/Application/Page.php 中)

	/**
	 * List of all articles page
	 */
	public function articlesAction() {
		$components = [
			'NAV'         => new \Sy\Bootstrap\Component\Article\Nav('articles'),
			'SEARCH_FORM' => new \Sy\Bootstrap\Component\Article\Search(),
			'FEED'        => new \Sy\Bootstrap\Component\Article\Feed(),
		];

		// Add article modal button
		$service = \Project\Service\Container::getInstance();
		if ($service->user->getCurrentUser()->hasPermission('article-create')) {
			$components['ADD_FORM'] = new \Sy\Bootstrap\Component\Article\Add(['class' => 'mb-3']);
		}

		$this->setContentVars($components);
	}

	/**
	 * Article page
	 */
	public function articleAction() {
		// Redirection if no article id provided
		$id = $this->get('id');
		if (is_null($id)) throw new \Sy\Bootstrap\Application\Page\NotFoundException();

		// Detect language
		$service = \Project\Service\Container::getInstance();
		$lang = $service->lang->getLang();

		// Retrieve article
		$article = $service->article->retrieve(['id' => $id, 'lang' => $lang]);

		if (empty($article)) {
			$lang = LANG;
			$article = $service->article->retrieve(['id' => $id, 'lang' => $lang]);
		}
		if (empty($article)) throw new \Sy\Bootstrap\Application\Page\NotFoundException();

		// Article content
		$content = new \Sy\Bootstrap\Component\Article\Content($id, $lang);

		$this->setContentVars([
			'ARTICLE_BREADCRUMB' => new \Sy\Bootstrap\Component\Article\Breadcrumb($id, $lang),
			'ARTICLE_CONTENT'    => $content,
			'ARTICLE_AUTHOR'     => new \Sy\Bootstrap\Component\Article\Author($article['user_id']),
			'SIDE'               => new \Sy\Bootstrap\Component\Article\Side($id, $article['category_id']),
			'SHARE'              => new \Sy\Bootstrap\Component\Share\Buttons(PROJECT_URL . Url::build('page', 'article', ['id' => $id])),
		]);
	}

在 Application.php 中添加 URL 转换器

protected/src/Application.php

<?php
namespace Project;

use Sy\Bootstrap\Lib\Url;

class Application extends \Sy\Bootstrap\Application {

	protected function initUrlConverter() {
		Url\AliasManager::setAliasFile(__DIR__ . '/../conf/alias.php');
		Url::addConverter(new Url\AliasConverter());
		Url::addConverter(new Url\ArticleConverter()); // Add article converter
		Url::addConverter(new Url\ControllerActionConverter());
	}

}