fsmdev/laravel-page-attributes 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fsmdev/laravel-page-attributes

Composer 安装命令:

composer require fsmdev/laravel-page-attributes

包简介

Tool for manage meta and other page attributes for seo

README 文档

README

English | Русский

Package for Laravel 5, that helps manage metadata and other page attributes, generate html tags, get page attributes from data base and make own page attributes and html templates for they.

Installation

composer require fsmdev/laravel-page-attributes
For Laravel 5.4 and earlier versions

If you use Laravel 5.5 or higher package discover automatically. For earlier versions add provider and alias in config/app.php file.

# config/app.php

'providers' => [
  ...
  Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider::class,
  ...
];

'aliases' => [
  ...
  'PageAttributes' => Fsmdev\LaravelPageAttributes\Facades\PageAttributes::class,
  ...
];

Configuration

If you want to change multilanguage mode this can be done by setting the value of the FSMDEV_MULTI_LANGUAGE property in the .env file. By default multi language mode is disabled (false).

For changin

To change other settings, use the file config/page_attributes.php. The file can be created manually or using the command:

php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=config

Basic usage

Most of the operations described below are performed using the facade PageAttributes.

use Fsmdev\LaravelPageAttributes\Facades\PageAttributes;

Setting page attributes

To set the attributes of the page (for example, metadata), the set method of the PageAttributes facade is used.

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');

Default values

You can set default values for page attributes. Initially, charset and viewport attributes have default values.

# config/page_attributes.php

'default' => [

  # Default value for title
  'title' => 'Awesome Page',
  
  # Override charset
  'charset' => 'windows-1251',
],

Getting page attributes

To get the page attribute values, use the get method of the PageAttributes facade.

get ( string $name) : string

Getting html

To get html attributes of the page, the method html of the PageAttributes facade is used.

html ( string $name) : string

You can also use Blade directives: @charset, @viewport, @title, @description, @keywords, @canonical.

{{ PageAttributes::html('title') }}

or

@title

Creating your own templates

The html method uses the predefined html code templates to generate the result. These templates can override or add new templates for your own attributes.

# 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>',
],

The configuration specified above can be used as follows.

Controller:

PageAttributes::set('my_attribute', 'My Value');

View:

{{ PageAttributes::html('my_attribute') }}

Page output:

<p>My Value</p>

Default view usage

The package includes a view that displays the following tags: charset, viewport, title, description, keywords, canonical. To use it, add it to the <head> block of the page.

@include('page_attributes::meta')

To change the view you need to create a file resouces/views/vendor/page_attributes/meta.blade.php. You can do this automatically with the command:

php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=view

Overriding the model used in the facade

Change configuration parameter class for change model that uses in PageAttributes facade.

Page Context usage

For separation data and view, it is better to store metadata in a database. When a page is associated with an object of some kind of model, it is easily solved by adding metadata fields to the model. But what to do for pages like main or categories? The page context mechanism offers a solution.

Installation

php artisan vendor:publish --provider="Fsmdev\LaravelPageAttributes\PageAttributesServiceProvider" --tag=context

php artisan migrate

After installation, a file will appear in the app/ConstantsCollections folder with the PageAttributesContext class inherited from ConstantCollection, the table_attributes table will also appear in the database.

Conllection: PageAttributesContext

In this class, you must specify a set of constants corresponding to the pages for which the mechanism for obtaining attributes by context will be used. Constant values must be of type TYNIINT UNSIGNED. It is also recommended to set the names of the constants in the propertiesName method. These names can be further used to create a CRUD of the PageAttribute class.

You can read more about working with the ConstantsCollection class 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',
    ];
}

Model: PageAttribute

The package includes a model Fsmdev\LaravelPageAttributes\Models\PageAttribute with table page_attributes. The table (model) contains the following fields:

context (UNSIGNED TINYINT) - contains the value of the constant corresponding to the context for which the attribute is being set.

language (CHAR(2) NULLABLE) - language / locale for which the attribute value is specified. If the use of multi_language is set to false, this attribute is not taken into account when retrieving data.

name (char(30)) - attribute name.

value (text) - attribute value.

Fields context, language and name form a unique index and are key to defining an attribute value.

An example of filling data:

context language name value
5 NULL h1 Awesome Site
5 NULL title Welcome to Awesome Site
5 NULL my_attribute My Value
15 NULL h1 My Blog
Context usage

To set the page attribute context, use the context method of the PageAttributes facade.

context ( integer $context) : void
PageAttributes::context(PageAttributesContext::INDEX);

When the context is set, the get, html methods and blade directives will use the PageAttribute model to find the values of the required attribute:

{{ PageAttributes::html('title'); }}

Returns (for the case when the data is filled as in the table above):

<title>Welcome to Awesome Site</title>

Variables

In page attributes you can use variables. Default syntax:

{--variable_name--}

Opening and closing symbols of variable you can change in configuration parameters variable_open and variable_close.

Variable value set

For variable value set this methods of PageAttributes facade is used:

context ( integer $context, array|null $variables = []) : void

variables ( array $variables) : void

variable ( string $name, string $value) : void

First 2 can get array as parameter. Keys of array are variables names.

Default values

Default values of variables can be set in configuration parameter default_variables.

Example

For page context POST_SHOW set title attribute:

{--post_name--} | Blog | {--site_name--}

Code:

# 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'
]);

Result in title:

About Me | Blog | Awesome Site

The priority of attribute value selection

When determining the attribute value, the source priority is as follows:

  1. Attributes set directly by the method set;
  2. Attributes received by context (if set);
  3. Attribute default values.

fsmdev/laravel-page-attributes 适用场景与选型建议

fsmdev/laravel-page-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「seo」 「laravel」 「meta」 「laravel meta」 「laravel seo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fsmdev/laravel-page-attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fsmdev/laravel-page-attributes 我们能提供哪些服务?
定制开发 / 二次开发

基于 fsmdev/laravel-page-attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 76
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-14