Git Product home page Git Product logo

laravel-meta-tags's Introduction

Development and support of this package is discontinued! Use a better solution fomvasss/laravel-seo

Laravel Meta Tags

License Build Status Latest Stable Version Total Downloads Quality Score

With this package you can manage meta-tags and SEO-fields from Laravel controllers and "blade" template.


Installation

Run from the command line:

composer require fomvasss/laravel-meta-tags

Publish and settings

  1. Publish assets - run this on the command line:
php artisan vendor:publish --provider="Fomvasss\LaravelMetaTags\ServiceProvider"
  • A configuration file will be publish to config/meta-tags.php.
  • A migration file will be publish to database/migrations/DATE_NOW_create_meta_tags_table.php.
  • A customizable blade template file will be publish to resources/views/vondor/meta-tags/tags.blade.php.
  1. Edit assets:
  • Set available tags inconfig/meta-tags.php - uncomment needed
  • If needed - set own model class for meta-tags inconfig/meta-tags.php
  • Edit migration meta_tags file - set available field tags - uncomment needed
  1. Run migration
php artisan migrate

Upgrading

When upgrading from v2 to v3, please see the UPGRADING.md


Integrate & usage

Usage in Eloquent models: app/Models/Article.php

Add Metatagable trait in your entity model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Fomvasss\LaravelMetaTags\Traits\Metatagable;

class Article extends Model
{
    use Metatagable;
    //...
}
app/Http/Controllers/ArticleController.php:

Usage facade MetaTag in controllers: app/Http/Controllers/ArticleController.php

<?php 

namespace App\Http\Controllers;

use MetaTag;

class ArticleController extends Controller 
{
    public function index()
    {
        $articles = \App\Model\Article::paginate();
        
        MetaTag::setTags([
            'title' => 'Article index page',
            'description' => 'It is article index page',
        ]);

        return view('index', compact('articles'));
    }
    
    public function store(Request $request)
    {
    	// create entity
        $article = \App\Model\Article::create($request->only([
            //.. article data
        ]));

		// create meta tag for entity
        $article->metaTag()->create($request->only([
            //.. meta tags fields
        ]));
    }

    public function show($id)
    {
        $article = \App\Model\Article::findOrFail($id);
        
        // Set tags for showing
        MetaTag::setEntity($article)
            ->setDefault([
                'title' => $article->title, // if empty $article->metaTag->title - show this title
			])->setTags([
				'seo_text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
				'h1' => $article->title,   
			]);
        
        return view('stow', compact('article'));
    }

    public function search(Request $request)
    {
        $articles = \App\Model\Article::bySearch($request->q)
            ->paginate();
        
        // Set tags for showing
        MetaTag::setPath()  // if argument `setPath()` is empty (or not set) - path = `request()->path()`
            ->setDefault([
                'title' => 'Search page',
                'robots' => 'noindex',
                'og_title' => 'Search page OG',
                'twitter_title' => 'Search page Twitter',
                'canonical' => 'page/search',
            ]);
        
        return view('index', compact('articles'));
    }
}

For the package to work correctly, you must save to the database, in the path field, only the url-path itself, without a domain and trim slash'es (/)

Example:

  • https://site.com/some/pages/?page=23 => some/pages
  • https://site.com/some/pages => /

Usage facade MetaTag in blade templates: resources/views/layouts/app.blade.php

Simple and efficient:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8">

        {!! MetaTag::render() !!}
        
    </head>
    <body>
        @yield('content')
    </body>
</html>

Or output one by one manually:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8">

        <title>{!! MetaTag::tag('title') !!}</title>
        <meta name="description" content="{!! MetaTag::tag('description') !!}">
        <meta name="keywords" content="{!! MetaTag::tag('keywords') !!}">
        
    </head>
    <body>
        @yield('content')
    </body>
</html>

Another example: resources/views/articles/show.blade.php

@extends('layouts.app')
@section('content')
	<h1>{!! MetaTag::tag('title') !!}</h1>
	<div>{!! $article->body !!}</div>
	<div>{{ MetaTag::tag('seo_text') }}</div>
@endsection

And you can set meta tags right in the template:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        
        @php(MetaTag::setEntity($article))
        @php(MetaTag::setDefault(['description' => 'My default meta tag']))
        
        {!! MetaTag::render() !!}
        
    </head>
    <body>
        @yield('content')
    </body>
</html>

Similarly:

{!!
    \MetaTag::setEntity($article)
        ->setDefault(['description' => 'My default meta tag'])
        ->render()
    !!}
{!! 
    \MetaTag::setPath('articles')
        ->setDefault(['robots' => 'follow', 'canonical' => 'page/articles'])
        ->setDefault(['title' => 'All articles'])
        ->setDefault(['og_title' => 'All articles'])
        ->setDefault(['og_locale' => 'de'])
        ->setDefault(['og_image' => 'files/images/5be3d92e02a55890e4301ed4.jpg', 'og_image_height' => 123])
        ->render() 
!!}

Links

laravel-meta-tags's People

Contributors

fomvasss avatar juliomotol avatar romanits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-meta-tags's Issues

Breaking Change From v2 -> v3

We are planning to upgrade our application. Right now its currently using fomvasss/laravel-meta-tags v2.0.1. And while scrolling through the changes from 2.0.1...3.2.0, there seem to been an undocumented breaking change. See the following diffs:

In our application, the meta_tags table already have metatagable_id and metatagable_type. This wouldn't be a problem if the project is is still in development, but we have other projects that uses this package and is in production. This could potentially break our application.

For the meantime we've found a work around by extending the MetaTag model and overriding the $fillable property and metatagable() method and using the that model in the meta-tags.model config.

Render not working

fomvasss/laravel-meta-tags 3.0.0

expected last version is not curretly working

        MetaTag::setDefault([
            'viewport' => 'width=device-width, initial-scale=1, shrink-to-fit=no',
            'csrf-token' => csrf_token(),
            'title' => $title,
            'description' => $description,
            'author' => setting('site-author'),
            'keywords' => str_replace(' ', ',', strtolower($title)),
            //
            'og_type' => 'website',
            'og_url' => $fullUrl,
            'og_title' => $title,
            'og_description' => $description,
            'og_image' => $image,// app_logo('og_image'),
            //
            'twitter_title' => $title,
            'twitter_card' => 'summary_large_image',
            'twitter_site' => $fullUrl,
            'twitter_url' => $fullUrl,
            'twitter_description' => $description,
            'twitter_image' => $image,// app_logo('og_image'),
        ]);

        dd(__METHOD__, MetaTag::render());

Screenshot from 2019-10-03 09-50-54

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.