fsmdev/laravel-page-attributes

Tool for manage meta and other page attributes for seo

1.0.4 2020-01-31 05:25 UTC

This package is auto-updated.

Last update: 2025-04-29 01:00:31 UTC


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.