mediabet-concepture/yii2-handbook-module

正在开发中的包

安装: 640

依赖者: 0

建议者: 0

安全性: 0

类型:yii2-extension

v2.0.6 2020-08-24 09:04 UTC

This package is auto-updated.

Last update: 2024-09-15 09:49:21 UTC


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

对于需要在网站地图中的每个实体,在服务中添加 trait concepture\yii2handbook\services\traits\SitemapSupportTrait;并在 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

      动态索引文件(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']
                  ]
              ]
          ],
          ...
      ]
      ...