1. Go to this page and download the library: Download butschster/meta-tags library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
butschster / meta-tags example snippets
use Butschster\Head\MetaTags\MetaInterface;
class HomeController extends Controller
{
public function __contruct(
protected MetaInterface $meta
) {
}
public function index()
{
$news = News::paginate();
// Prepend title part to the default title
$this->meta
// Will render "Home page - Default Title"
->prependTitle('Home page')
// Will );
}
}
namespace App\Providers;
use Butschster\Head\Facades\PackageManager;
use Butschster\Head\Packages\Package;
use Illuminate\Support\ServiceProvider;
class MetaTagsServiceProvider extends ServiceProvider
{
...
protected function packages()
{
PackageManager::create('jquery', function(Package $package) {
$package->addScript(
'jquery.js',
'https://code.jquery.com/jquery-3.3.1.min.js',
['defer']
);
});
PackageManager::create('calendar', function(Package $package) {
$package->
use Butschster\Head\Facades\Meta;
class EventsController extends Controller {
public function show(Event $event)
{
// Will
Meta::setTitle('Laravel');
// <title>Laravel</title>
// You can specify max length. (By default it gets from config.)
Meta::setTitle('Laravel', 4);
// <title>Lara...</title>
Meta::addScript('script.js', 'http://site.com/script.js');
// <script src="http://site.com/script.js"></script>
// You can override or add attributes
Meta::addScript('script.js', 'http://site.com/script.js', ['async', 'defer', 'id' => 'hj2b3424iu2-dfsfsd']);
// <script src="http://site.com/script.js" async defer id="hj2b3424iu2-dfsfsd"></script>
// You can specify placement. By default, for scripts it's footer
Meta::addScript('script.js', 'http://site.com/script.js', [], 'custom_placement');
// <script src="http://site.com/script.js" async defer id="hj2b3424iu2-dfsfsd"></script>
class FacebookPixelTag implements \Butschster\Head\Contracts\MetaTags\Entities\TagInterface {
private $pixel_id;
public function __construct(string $id)
{
$this->pixel_id = $id
}
public function getPlacement(): string
{
return 'footer'
}
public function toArray()
{
return [
'type' => 'facebook_pixel_tag',
'pixel_id' => $this->pixel_id
];
}
public function toHtml()
{
return '<script type="text/javascript">...</script>'
}
}
Meta::addTag('facebook.pixel', new FacebookPixelTag('42b3h23-34234'));
// <script type="text/javascript">...</script>
$tags = new \Butschster\Head\MetaTags\TagsCollection([
...
]);
Meta::registerTags($tags);
// You can specify the placement
Meta::registerTags($tags, 'footer');
Meta:::load registered packages with names jquery and vuejs and append tags from there to Meta
Meta::setTitle('Current title');
$package = new \Butschster\Head\Packages\Package('custom_package');
$package->setTitle('New title');
// Will replace "Current title" to "New title" after package registration
Meta::registerPackage($package);
$package = new \Butschster\Head\Packages\Package('custom_package');
$package->setTitle('Custom title');
$newPackage = new \Butschster\Head\Packages\Package('custom_package');
$newPackage->setTitle('New title');
// Will replace "Current title" to "New title" after package registration
Meta::registerPackage($package);
Meta::replacePackage($newPackage);
$package = new \Butschster\Head\Packages\Package('custom_package');
$package->setTitle('Custom title');
// Will replace "Current title" to "New title" after package registration
Meta::registerPackage($package);
Meta::removePackage('custom_package');
$package = new \Butschster\Head\Packages\Package('custom_package');
$package->setTitle('Custom title');
// Will replace "Current title" to "New title" after package registration
Meta::registerPackage($package);
$package = Meta::getPackage('custom_package');
namespace App;
use Butschster\Head\Contracts\MetaTags\SeoMetaTagsInterface;
use Butschster\Head\Contracts\MetaTags\RobotsTagsInterface;
class Page extends Model implements SeoMetaTagsInterface, RobotsTagsInterface {
public function getTitle(): ?string
{
return $this->title;
}
public function getDescription(): ?string
{
return $this->description;
}
public function getKeywords()
{
return $this->keywords;
}
public function getRobots(): ?string
{
return 'noindex, nofollow';
}
}
// Controller
use Butschster\Head\Facades\Meta;
class PageController extends Controller {
public function show(\App\Page $page)
{
Meta::setMetaFrom($page);
}
}
//Service Provider
Meta::macro('registerSeoMetaTagsForPage', function (\App\Page $page) {
$this
->prependTitle($page->title)
->setKeywords($page->meta_keywords)
->setDescription($page->meta_description);
});
// Controller
use Butschster\Head\Facades\Meta;
class PageController extends Controller {
public function show(\App\Page $page)
{
Meta::registerSeoMetaTagsForPage($page);
}
}
$package = new \Butschster\Head\Packages\Package('jquery');
PackageManager::register($package);
// or
PackageManager::create('jquery', function($package) {
...
});
$package = new \Butschster\Head\Packages\Package('jquery');
$package->getName(); // jquery
$package = new \Butschster\Head\Packages\Package('jquery');
$package->addStyle('style.css', 'http://site.com/style.css');
// <link media="all" type="text/css" rel="stylesheet" href="http://site.com/style.css" />
// You can change or add attributes
$package->addStyle('style.css', 'http://site.com/style.css', [
'media' => 'custom', 'defer', 'async'
]);
// <link media="custom" type="text/css" rel="stylesheet" href="http://site.com/style.css" defer async />
$package = new \Butschster\Head\Packages\Package('jquery');
$package->addScript('script.js', 'http://site.com/script.js');
// <script src="http://site.com/script.js"></script>
// You can change or add attributes
$package->addScript('script.js', 'http://site.com/script.js', ['async', 'defer', 'id' => 'hj2b3424iu2-dfsfsd']);
// <script src="http://site.com/script.js" async defer id="hj2b3424iu2-dfsfsd"></script>
// You can placement. By default it's footer
$package->addScript('script.js', 'http://site.com/script.js', [], 'custom_placement');
// <script src="http://site.com/script.js" async defer id="hj2b3424iu2-dfsfsd"></script>
Meta::
$og = new Butschster\Head\Packages\Entities\OpenGraphPackage('some_name');
$og->setType('website')
->setSiteName('My awesome site')
->setTitle('Post title');
// You can render itself
$og->toHtml();
// <meta name="og:type" content="website">
// <meta name="og:site_name" content="My awesome site">
// <meta name="og:title" content="Post title">
// Or just register this package in Meta class and it will be rendered automatically
Meta::registerPackage($og);
$card = new Butschster\Head\Packages\Entities\TwitterCardPackage('some_name');
$card->setType('summary')
->setSite('@username')
->setTitle('Post title');
// You can render itself
$card->toHtml();
// <meta name="twitter:card" content="summary">
// <meta name="twitter:site" content="@username">
// <meta name="twitter:title" content="Post title">
// Or just register this package in Meta class and it will be rendered automatically
Meta::registerPackage($card);
$title = new \Butschster\Head\MetaTags\Entities\Title('Lorem Ipsum is simply dummy text of the printing and typesetting');
$title->setMaxLength(20);
$title->toHtml(); // <title>Lorem Ipsum is simpl...</title>
use Butschster\Head\MetaTags\Entities\Script;
$script = new Script('jquery.js', 'http://site.com/script.js', ['defer', 'async']);
$script->toHtml();
// <script src="http://site.com/script.js" defer async></script>
Meta::addTag($script);
use Butschster\Head\MetaTags\Entities\Style;
$style = new Style('style.css', 'http://site.com/style.css');
$style->toHtml();
// <link media="all" type="text/css" rel="stylesheet" href="http://site.com/style.css" />
Meta::addTag($style);
use Butschster\Head\MetaTags\Entities\Comment;
use Butschster\Head\MetaTags\Entities\Favicon;
$favicon = new Favicon('http://site.com/favicon.ico');
$comment = new Comment($favicon, 'Favicon');
Meta::addTag('favicon', $comment);
// Will render
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="http://site.com/favicon.ico" />
<!-- /Favicon -->
use Butschster\Head\MetaTags\Entities\ConditionalComment;
use Butschster\Head\MetaTags\Entities\Favicon;
$favicon = new Favicon('http://site.com/favicon.ico');
$comment = new ConditionalComment($favicon, 'IE 6');
Meta::addTag('favicon', $comment);
<!--[if IE 6]>
<link rel="icon" type="image/x-icon" href="http://site.com/favicon.ico" />
<![endif]-->
use Butschster\Head\MetaTags\Entities\GoogleAnalytics;
$script = new GoogleAnalytics('UA-12345678-1');
Meta::addTag('google.analytics', $script);
use Butschster\Head\MetaTags\Entities\GoogleTagManager;
$script = new GoogleTagManager('UA-12345678-1');
Meta::addTag('google.tagmanager', $script);
use \Butschster\Head\MetaTags\Entities\YandexMetrika;
$script = new YandexMetrika('20925319');
Meta::addTag('yandex.metrika', $script);
$script = new YandexMetrika('20925319');
$script->clickmap(bool);
$script = new YandexMetrika('20925319');
$script->webvisor(bool);
$script = new YandexMetrika('20925319');
$script->trackLinks(bool);
$script = new YandexMetrika('20925319');
$script->accurateTrackBounce(bool);
$script = new YandexMetrika('20925319');
$script->trackHash(bool);
$script = new YandexMetrika('20925319');
$script->eCommerce(string $containerName);
$script = new YandexMetrika('20925319');
$script->useCDN();
$script = new YandexMetrika('20925319');
$script->disableNoScript();
use \Butschster\Head\MetaTags\Entities\JavascriptVariables;
$variables = new JavascriptVariables([
'string' => 'Hello world',
'number' => 4815162342,
'bool' => true,
'nullable' => null
]);
// you can put new variable
$variables->put('array', ['jquery', 'vuejs']);
Meta::addTag('variables', $variables);
use \Butschster\Head\MetaTags\Entities\JavascriptVariables;
$variables = new JavascriptVariables([
'string' => 'Hello world',
'number' => 4815162342,
], 'custom');
namespace App\Providers;
use Butschster\Head\MetaTags\Entities\Favicon;
use Butschster\Head\MetaTags\Entities\ConditionalComment;
use Butschster\Head\Facades\PackageManager;
use Butschster\Head\Packages\Package;
class MetaTagsServiceProvider extends ServiceProvider {
...
protected function packages()
{
PackageManager::create('favicons', function(Package $package) {
$sizes = ['16x16', '32x32', '64x64'];
foreach ($sizes as $size) {
$package->addTag(
'favicon.'.$size,
new Favicon('http://site.com/favicon-'.$size.'.png', [
'sizes' => $size
])
);
}
$package->addTag('favicon.ie', new ConditionalComment(
new Favicon('http://site.com/favicon-ie.png'), 'IE gt 6'
));
});
}
...
}
...
'packages' => [
'favicons'
],
...
namespace App\Providers;
use Butschster\Head\MetaTags\Meta;
use Butschster\Head\Contracts\MetaTags\MetaInterface;
use Butschster\Head\Contracts\Packages\ManagerInterface;
class MetaTagsServiceProvider {
...
protected function registerMeta(): void
{
$this->app->singleton(MetaInterface::class, function () {
$meta = new Meta(
$this->app[ManagerInterface::class],
$this->app['config']
);
// It just an imagination, you can automatically
// add favicon if it exists
if (file_exists(public_path('favicon.ico'))) {
$meta->setFavicon('/favicon.ico');
}
$meta->