pyrobyteweb/meta-templates

此包最新版本(dev-master)没有可用的许可证信息。

dev-master 2022-01-11 07:22 UTC

This package is auto-updated.

Last update: 2024-09-11 13:20:16 UTC


README

Laravel 8+
PHP 7.4+

安装

  1. 在文件 app.php 中,在 packages 部分添加: \PyrobyteWeb\MetaTemplates\MetaTemplatesServiceProvider::class
  2. php artisan vendor:publish --provider="PyrobyteWeb\MetaTemplates\MetaTemplatesServiceProvider"
  3. 在 Http/Kernel.php 中添加中间件 MetaTemplateMiddleware::class

工作原理

存在一些通用的占位符,您可以自行添加。也存在一些针对特定页面使用的占位符,它们基于路由名称。所有占位符都通过 # 来命名。例如,#year#

使用方法

为整个路由添加新元模板

要添加新类型的元模板,需要运行命令 php artisan meta-template:add name_meta_template。将为添加元模板到数据库创建迁移,并在 app/MetaTemplates 目录中创建文件。

添加新的占位符

运行命令 php artisan meta-template:placeholders PlaceHolderClassName

示例使用

为特定路由创建

我们将为测试页面创建元模板。

  1. 运行创建元模板的数据库命令 php artisan meta-template:add meta_template_for_testing_page
    将出现迁移,并将创建用于描述元模板的文件。
  2. 描述创建元模板的迁移。在示例中,将只描述要添加的属性。
private $metaTemplates = [
    [
        'name' => 'test page',
        'route_name' => 'test-route-another',
        'active' => 1,
        'meta_title' => 'test title - #year# #custom_placeholder#',
        'meta_keywords' => 'test - #moth#',
        'meta_description' => 'test - #time#',
    ],
];
  1. 在创建的类中描述我们的 #custom_placeholder#,我们将在哪里输出。例如,
public function getPlaceholders(): array
{
    return [
        'custom_placeholder' => 'мой кастомный плейсхолдер',
    ];
}
  1. 转到测试页面并调用我们的元模板 app('meta)->getTitle(),将会输出以下内容 test title - 2022 我的自定义占位符

创建占位符(通用)

  1. 运行创建数据库元模板的命令 php artisan meta-template:placeholders CommonMetaTemplate
    将出现描述元模板的文件。
  2. 在创建的类中描述我们的 #common_placeholder#,我们将在哪里输出。例如,
public function getPlaceholders(): array
{
    return [
        'common_placeholder' => 'мой кастомный плейсхолдер',
    ];
}
  1. 将我们的类 CommonMetaTemplate::class 添加到配置文件 config/meta-templates.php 中的 commod 部分。
  2. 现在我们可以在任何元模板中使用我们的占位符,而不必每次为特定页面重复描述。