fsmdel / laravel-page-attributes
用于管理元数据和页面SEO属性的工具体
Requires
- php: >=7.1.0
- fsmdev/constants-collection: ^1.0.0
README
用于Laravel 5的包,帮助管理元数据和页面属性,生成HTML标签,从数据库获取页面属性并创建自定义页面属性和HTML模板。
安装
composer require fsmdev/laravel-page-attributes
对于Laravel 5.4和更早版本
如果您使用Laravel 5.5或更高版本,包将自动发现。对于更早版本,请在config/app.php文件中添加provider和alias。
# config/app.php 'providers' => [ ... Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider::class, ... ]; 'aliases' => [ ... 'PageAttributes' => Fsmdev\LaravelPageAttributes\Facades\PageAttributes::class, ... ];
配置
如果您想更改多语言模式,可以通过在.env文件中设置FSMDEV_MULTI_LANGUAGE属性的值来实现。默认情况下,多语言模式是禁用的(false)。
更改
要更改其他设置,请使用config/page_attributes.php文件。该文件可以手动创建或使用以下命令创建:
php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=config
基本用法
以下大多数操作都是使用PageAttributes外观完成的。
use Fsmdev\LaravelPageAttributes\Facades\PageAttributes;
设置页面属性
要设置页面属性(例如,元数据),请使用PageAttributes外观的set方法。
set ( string|array $name, string $value ) : void
PageAttributes::set('title', 'Awesome Page'); PageAttributes::set('h1', $post->name); # Own attribute PageAttributes::set('my_attribute', 'My Value');
默认值
您可以设置页面属性的默认值。最初,charset和viewport属性有默认值。
# config/page_attributes.php 'default' => [ # Default value for title 'title' => 'Awesome Page', # Override charset 'charset' => 'windows-1251', ],
获取页面属性
要获取页面属性值,请使用PageAttributes外观的get方法。
get ( string $name) : string
获取HTML
要获取页面HTML属性,请使用PageAttributes外观的html方法。
html ( string $name) : string
您还可以使用Blade指令:@charset、@viewport、@title、@description、@keywords、@canonical。
{{ PageAttributes::html('title') }}
或
@title
创建自己的模板
html方法使用预定义的HTML代码模板生成结果。这些模板可以覆盖或为您的属性添加新模板。
# config/page_attributes.php 'html_templates' => [ # Overriding template for h1 'h1' => '<h1 class="some-class"><{value}/h1>', # Creating template for own attribute 'my_attribute' => '<p>{value}</p>', ],
上述配置可以按如下方式使用。
控制器
PageAttributes::set('my_attribute', 'My Value');
视图
{{ PageAttributes::html('my_attribute') }}
页面输出
<p>My Value</p>
默认视图使用
该包包含一个显示以下标签的视图:charset、viewport、title、description、keywords、canonical。要使用它,请将其添加到页面的<head>块中。
@include('page_attributes::meta')
要更改视图,需要创建一个文件resouces/views/vendor/page_attributes/meta.blade.php。您可以使用以下命令自动完成此操作:
php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=view
覆盖外观中使用的模型
更改配置参数class,以更改PageAttributes外观中使用的模型。
页面上下文使用
为了分离数据和视图,最好将元数据存储在数据库中。当一个页面与某种模型的对象相关联时,通过向模型添加元数据字段可以轻松解决。但对于主页或分类页面怎么办?页面上下文机制提供了一个解决方案。
安装
php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=context
php artisan migrate
安装后,将在app/ConstantsCollections文件夹中出现一个继承自ConstantCollection的PageAttributesContext类,数据库中也将出现table_attributes表。
集合:PageAttributesContext
在本类中,您必须指定一组与使用上下文获取属性机制相对应的页面常量。常量值必须是 TYNIINT UNSIGNED
类型。还建议在 propertiesName 方法中设置常量名称。这些名称可以进一步用于创建 PageAttribute 类的 CRUD。
有关 ConstantsCollection 类的使用,您可以在此处了解更多信息:here。
# app/ConstantsCollections/PageAttributesContext.php const INDEX = 5; const CONTACTS = 10; const BLOG = 15; protected static function propertiesName() { return [ self::INDEX => 'Home Page', self::CONTACTS => 'Contacts Page', self::BLOG => 'Blog', ]; }
模型:PageAttribute
该包包含一个模型 Fsmdev\LaravelPageAttributes\Models\PageAttribute
,对应于 page_attributes
表。该表(模型)包含以下字段
context(无符号 TINYINT)- 包含设置属性时对应的上下文常量值。
language(CHAR(2) 可空)- 指定的属性值的语言/区域。如果将 multi_language
设置为 false,则检索数据时不会考虑此属性。
name(char(30))- 属性名称。
value(text)- 属性值。
字段 context、language 和 name 形成一个唯一索引,是定义属性值的关键。
填写数据的示例
上下文使用
要设置页面属性上下文,请使用 PageAttributes 门面的 context 方法。
context ( integer $context) : void
PageAttributes::context(PageAttributesContext::INDEX);
设置上下文后,get、html 方法以及 blade 指令将使用 PageAttribute 模型来查找所需的属性值
{{ PageAttributes::html('title'); }}
返回结果(对于如上表所示填写的数据)
<title>Welcome to Awesome Site</title>
变量
在页面属性中,您可以使用变量。默认语法
{--variable_name--}
您可以在配置参数 variable_open
和 variable_close
中更改变量的开头和结尾符号。
变量值设置
用于设置变量值的 PageAttributes 门面使用以下方法
context ( integer $context, array|null $variables = []) : void
variables ( array $variables) : void
variable ( string $name, string $value) : void
前两个方法可以获取数组作为参数。数组的键是变量名称。
默认值
变量的默认值可以在配置参数 default_variables
中设置。
示例
对于页面上下文 POST_SHOW 设置标题属性
{--post_name--} | Blog | {--site_name--}
代码
# config/page_attributes.php 'default_variables' => [ 'site_name' => 'Awesome Site', ], # Controller PageAttributes::context(PageAttributesContext::POST_SHOW, [ 'post_name' => $post->name, // F.e. Post Name is 'About Me' ]);
标题中的结果
About Me | Blog | Awesome Site
属性值选择优先级
在确定属性值时,源优先级如下
- 通过方法 set 直接设置的属性;
- 通过上下文接收的属性(如果已设置);
- 属性默认值。