concepture / yii2-handbook-module
开发中的包
Requires
- php: >=5.6.0
- ramsey/uuid: ^3.9
- symfony/intl: ^4.4
- symfony/process: 4.4.4
- udokmeci/yii2-beanstalk: ^1.0
- yiisoft/yii2: 2.0.29
- yiisoft/yii2-jui: ~2.0.0
README
用于按域名划分内容的模块。包含可以用来划分内容的域名实体
连接
"require": { "concepture/yii2-handbook-module" : "*" },
执行迁移 php yii migrate/up --migrationPath=@concepture/yii2handbook/console/migrations
连接后台模块
'modules' => [
'handbook' => [
'class' => 'concepture\yii2handbook\Module'
],
],
连接控制台应用程序模块
'modules' => [
'handbook' => [
'class' => 'concepture\yii2handbook\Module',
'controllerMap' => [
'sitemap' => 'concepture\yii2handbook\console\controllers\SitemapController',
'url-history' => 'concepture\yii2handbook\console\controllers\UrlHistoryController',
]
],
],
生成网站地图
php yii handbook/sitemap/generate
完整重新生成网站地图
php yii handbook/sitemap/re-generate
对于应该在网站地图中的每个实体,在服务中添加 concepture\yii2handbook\services\traits\SitemapSupportTrait; trait 并在 afterModelSave 和 afterDelete 中实现 sitemapRefresh 方法的调用
!!! 在 frontend/web 中,为了获取网站地图,必须存在 xml 样式
为实体生成 URL 历史
php yii handbook/url-history/re-generate
对于应该在历史记录中的每个实体,在服务中添加 UrlHistoryInterface 接口并在 afterModelSave 中实现 refresh 方法的调用
Yii::$app->urlHistoryService->refresh($model, null, 'site', 'page', ['route' => 'seo_name']);
在 common/config/params-local.php 中添加参数,其中需要指定域名(不带 http)到 domains 表中的别名对应关系
<?php return [ 'yii2handbook' => [ 'domainMap' => [ 'example1.loc' => 'example1', 'example2.loc' => 'example2' ] ] ];
从表中获取当前本地化的 id Yii::$app->localeService->getCurrentLocaleId()
从表中获取当前域名的 id Yii::$app->domainService->getCurrentDomainId();
获取语言数组 Yii::$app->localeService->catalog();
获取域名数组 Yii::$app->domainService->catalog();
获取键的配置 Yii::$app->settingsService->catalogValue($key);
动态元素
-
为了获取页面的 SEO 设置,调用
Yii::$app->dynamicElementsService->getDataSet($model = null);
该方法将返回包含以下内容的 concepture\yii2handbook\datasets\SeoData 对象:
- 默认 SEO 数据,其中未指定特定 URL
- 如果传递了模型,将考虑模型的 SEO 数据
- 最后,将考虑当前 URL 的元素
-
Twig 扩展 - 在 Twig 配置中添加扩展
... 'extensions' => 'concepture\yii2handbook\twig\DynamicElementsExtension' ...
v2
... 'extensions' => 'concepture\yii2handbook\v2\twig\DynamicElementsExtension' ...
-
示例
{{ de(de_constant('SettingsTypeEnum::TEXT'), de_constant('SeoSettingEnum::TITLE'), _('de','Главная страница'), 'Заголовок главной страницы') }} {{ de(de_constant('SettingsTypeEnum::TEXT_AREA'), 'MY_TEXT_AREA', _('de','Произвольный текст'), 'Некий текст') }} {{ de(de_constant('SettingsTypeEnum::TEXT_EDITOR'), 'MY_EDITOR', _('de','Произвольный текст'), 'Некий текст 2') }}
v2
{{ de(de_const('Type::TEXT'), de_const('Name::TITLE'), 'Title', {'value' : 'Index title', 'no_control' : true}) }} {{ de(de_const('Type::TEXT_AREA'), de_const('Name::DESCRIPTION') , 'Description', {'value' : 'Index descriptions', 'no_control' : true}) }} {{ de(de_const('Type::TEXT_AREA'), de_const('Name::KEYWORDS'), 'Keywords', {'value' : 'Index keywords', 'no_control' : true}) }} {{ de(de_const('Type::TEXT'), 'MULTIPLE_DOMAIN', 'Test multiple domain', {'value' : 'Test multiple domain', 'multi_domain' : false}) }} {{ de(de_const('Type::TEXT'), 'MULTIPLE_DOMAIN_2', 'Test multiple domain', {'value' : 'Test multiple domain', 'general' : true, 'multi_domain' : false}) }} {{ de(de_const('Type::TEXT'), 'PARAMS', 'Test multiple domain', {'value' : 'Test {token} params', 'value_params' : {'token' : 'abc'}}) }}
-
控制台命令
- 连接(例如 console\config\main.php)
[ ... 'modules' => [ ... 'handbook' => [ 'class' => 'concepture\yii2handbook\Module', 'controllerMap' => [ ... 'dynamic-elements' => 'concepture\yii2handbook\console\controllers\DynamicElementsCommand', ... ] ], ... ] ... ]
- 调用示例
php yii handbook/dynamic-elements/replacement / /replacement --alias=alias
- 连接(例如 console\config\main.php)
动态索引文件(robots.txt)
连接
- 在必要的控制器中连接操作(例如 frontend\controllers\SiteController.php)
public function actions() { $actions = parent::actions(); ... $actions['robots'] = [ 'class' => 'concepture\yii2handbook\actions\RobotsAction', ]; ... return $actions; }
- 在路由中指定规则(例如 frontend\config\routes.php)
return [ ... [ 'pattern' => 'robots', 'route' => 'site/robots', 'suffix' => '.txt' ], ... ];
域名路由
- 连接(例如 frontend\config\main.php)
... 'components' => [ 'urlManager' => [ 'class' => 'concepture\yii2handbook\components\routing\DomainUrlManager', 'rules' => require __DIR__ . '/routes.php', ... ], ], ...
- 路由规则(例如 frontend\config\routes.php)
... [ 'patterns' => [ DomainEnum::A => 'objects', DomainEnum::B => 'subjects', ], 'route' => 'object/index' ], [ 'patterns' => [ DomainEnum::A => "objects/<seo_name:({$seo_name_regexp})>", DomainEnum::B => "subjects/<seo_name:({$seo_name_regexp})>", ], 'route' => 'object/view' ], [ 'pattern' => 'robots', 'route' => 'site/robots', 'suffix' => '.txt' ] ...
Hreflag 多域名
- 在配置中连接(例如 frontend\config\main.php)
... 'view' => [ ... 'renderers' => [ 'twig' => [ ... 'extensions' => [ ... 'concepture\yii2handbook\components\routing\HreflangExtension' ... ], ... ], ] ... ], ...
- 在模板中连接(frontend\views\layout\main.html.twig)
... <head> ... {{ hreflang() }} ... </head> ...
- 在 common/config/params-local.php 中连接域名(例如)参数 hreflang 包含域名
return [ 'yii2handbook' => [ 'domainMap' => [ 'alias.loc' => [ 'alias' => 'alias', 'locale' => 'ru', 'hreflang' => 'ru-ru' ] ] ] ];
控制器排序模块
- 创建对象实体(服务实体 - 表
entity_type
) - 在控制器中连接(例如 backend\controllers\ObjectController)
... use concepture\yii2handbook\actions\PositionSortIndexAction; use kamaelkz\yii2admin\v1\actions\EditableColumnAction; use kamaelkz\yii2admin\v1\actions\SortAction; use concepture\yii2handbook\services\EntityTypePositionSortService; use kamaelkz\yii2admin\v1\controllers\traits\ControllerTrait; ... public function actions(): array { $actions = parent::actions(); return array_merge($actions,[ ... PositionSortIndexAction::actionName() => [ 'class' => PositionSortIndexAction::class, 'entityColumns' => [ 'id', 'name', 'seo_name', ], 'labelColumn' => 'name', ], EditableColumnAction::actionName() => [ 'class' => EditableColumnAction::class, 'serviceClass' => EntityTypePositionSortService::class ], SortAction::actionName() => [ 'class' => SortAction::class, 'serviceClass' => EntityTypePositionSortService::class ] ... ]); } ...
- 在视图中连接(backend\object\index.php)
... use concepture\yii2handbook\actions\PositionSortIndexAction; ... $this->viewHelper()->pushPageHeader( [PositionSortIndexAction::actionName()], Yii::t('yii2admin', 'Сортировка'), 'icon-sort' ); ...
Beanstalk 队列管理器
缓存组件
SEO 块
- 在配置中连接(例如 frontend\config\main.php)
... 'view' => [ ... 'renderers' => [ 'twig' => [ ... 'extensions' => [ ... 'concepture\yii2handbook\twig\SeoBlockExtension' ... ], ... ], ] ... ], ...
- 在模板中连接(frontend\views\layout\main.html.twig)
... <head> ... {{ seo_block(c('SeoBlockPositionEnum::HEAD')) }} ... </head> ...
- 在后台中连接菜单元素
... 'yii2admin-navigation' => [ ... [ 'label' => Yii::t('yii2admin', 'SEO блоки'), 'url' => ['/handbook/seo-block/index'], 'active' => [ 'rules' => [ 'c' => ['seo-block'] ] ] ], ... ] ...