用于在 Laravel 项目中管理 SEO 元标签的最强大、最可扩展的工具
Laravel SEO Meta Tags offers a sophisticated solution for Laravel applications, allowing developers to seamlessly manage header meta tags, CSS, JavaScript, and other relevant tags. Its primary objective is to simplify the process of managing search engine optimization (SEO) tags within your application.
Moreover, its versatile API makes it compatible with frameworks like Inertiajs, VueJS and other JavaScript frameworks.
For any questions or further assistance, please join our official telegram group
We enthusiastically invite you to contribute to the package! Whether you've uncovered a bug, have innovative feature suggestions, or wish to contribute in any other capacity, we warmly welcome your participation. Simply open an issue or submit a pull request on our GitHub repository to get started.
Remember, every great developer was once a beginner. Contributing to open source projects is a step in your journey to becoming a better developer. So, don't hesitate to jump in and start contributing!
We appreciate any contributions to help make the package better!
Pair it with our tool for a more robust development environment. For more information visit buggregator.dev
composer require butschster/meta-tagsYou can do it using the following artisan command:
php artisan meta-tags:installThis command will activate the App\Providers\MetaTagsServiceProvider and publish the configuration
at config/meta_tags.php. Within this configuration file, you can set default titles, keywords, descriptions, and other
meta tags which will be automatically inserted into your HTML.
App\Providers\MetaTagsServiceProvider has been added to the providers array in
your config/app.php configuration file. If it hasn't, you'll need to add it manually. Remember, if your application
isn't using the App namespace, update the provider class name accordingly.And that's all! Your Laravel project is now equipped to handle SEO meta tags with ease.
You can use either Facade \Butschster\Head\Facades\Meta or \Butschster\Head\Contracts\MetaTags\MetaInterface in your
controller
…To integrate meta tags into your HTML, simply insert {!! Meta::toHtml() !!} wherever required.
Note You have two options to insert meta tags:
{!! Meta::toHtml() !!}or the Blade directive@meta_tags.
Here's an example of how you can use it in your view:
{!! Meta::toHtml() !!}
...
The package provides flexibility to insert meta tags beyond the header. You can define specific placements in your templates.
Meta::footer()->toHtml()Meta::placement('placement_name')->toHtml()@meta_tags('placement_name')
...
{!! Meta::placement('middle_of_the_page')->toHtml() !!}
...
{!! Meta::footer()->toHtml() !!}
To avoid code repetition and improve code organization, you can group tags, assets, etc., into named packages. This allows for streamlined inclusion of sets of meta tags or assets where needed.
To create a new package:
\App\Providers\MetaTagsServiceProvider.To define packages, you can use the PackageManager::create method:
…When specific packages are required within a controller, you can include them by referencing the package name:
use Butschster\Head\Facades\Meta;
class EventsController extends Controller {
public function show(Event $event)
{
// Will include all tags from calendar package
Meta::includePackages(['calendar', ...]);
}
}For packages that should be included on every page, you can define them in the config/meta_tags.php:
...
'packages' => [
'jquery', 'calendar', ...
],
...And there you have it! With these steps, handling and organizing meta tags and assets becomes a breeze.
Note All methods available in the
Metaclass can also be utilized alongside these package functions.
\Butschster\Head\MetaTags\Meta
Illuminate\Contracts\Support\Htmlable interfaceSet the main part of meta title
Meta::setTitle('Laravel');
// Laravel
// You can specify max length. (By default it gets from config.)
Meta::setTitle('Laravel', 4);
// Lara...Prepend title part to main title
Meta::setTitle('Laravel')
->prependTitle('Home page');
// Home page - LaravelSet the title separator
By default it gets from config
Meta::setTitleSeparator('->')
->setTitle('Laravel')
->prependTitle('Home page');
// Home page -> LaravelSet the description
Meta::setDescription('Awesome page');
//
// You can specify max length. (By default it gets from config.)
Meta::setDescription('Awesome page', 7);
// Set the keywords
Meta::setKeywords('Awesome keywords');
//
Meta::setKeywords(['Awesome keyword', 'keyword2']);
//
// You can specify max length. (By default it gets from config.)
Meta::setKeywords(['keyword', 'keyword2'], 10);
// Set the robots
Meta::setRobots('nofollow,noindex');
// Set the content type
Meta::setContentType('text/html');
//
Meta::setContentType('text/html', 'ISO-8859-1');
// Set the viewport
Meta::setViewport('width=device-width, initial-scale=1');
// Add webmaster tags You can add multiple tags
use Butschster\Head\MetaTags\Entities\Webmaster;
// Supported services [google, yandex, pinterest, alexa, bing]
Meta::addWebmaster(Webmaster::GOOGLE, 'f+e-Ww4=[Pp4wyEPLdVx4LxTsQ');
//
Meta::addWebmaster('yandex', 'f+e-Ww4=[Pp4wyEPLdVx4LxTsQ');
//
Meta::addWebmaster('bing', 'f+e-Ww4=[Pp4wyEPLdVx4LxTsQ');
//
Meta::addWebmaster('alexa', 'f+e-Ww4=[Pp4wyEPLdVx4LxTsQ');
//
Meta::addWebmaster(Webmaster::PINTEREST, 'f+e-Ww4=[Pp4wyEPLdVx4LxTsQ');
// Set the prev href
Meta::setPrevHref('http://site.com/prev');
// Set the next href
Meta::setNextHref('http://site.com/next');
// Set the canonical link
Meta::setCanonical('http://site.com');
// Set canonical link, prev and next from paginator object
$news = \App\News::paginate();
Meta::setPaginationLinks($news);
//
//
// Add a hreflang link
Meta::setHrefLang('en', http://site.com/en');
Meta::setHrefLang('ru', http://site.com/ru');
//
// Set the character encoding for the HTML document
Meta::setCharset();
//
Meta::setCharset('ISO-8859-1');
// Set the favicon
Meta::setFavicon('http://site.com/favicon.ico');
//
Meta::setFavicon('http://site.com/favicon.png');
//
Meta::setFavicon('http://site.com/favicon.gif');
//
Meta::setFavicon('http://site.com/favicon.svg');
//
//You can set additional attributes
Meta::setFavicon('http://site.com/favicon.svg', ['sizes' => '16x16', 'type' => 'custom_type']);
// Add a custom link tag
Meta::addLink('apple-touch-icon-precomposed', [
'href' => 'http://site.com',
'id' => 'id:213'
]);
// Add a link to a css file
Meta::addStyle('style.css', 'http://site.com/style.css');
//
// You can override or add attributes
Meta::addStyle('style.css', 'http://site.com/style.css', [
'media' => 'custom', 'defer', 'async'
]);
// Add a link to a script file
Meta::addScript('script.js', 'http://site.com/script.js');
//
// You can override or add attributes
Meta::addScript('script.js', 'http://site.com/script.js', ['async', 'defer', 'id' => 'hj2b3424iu2-dfsfsd']);
//
// You can specify placement. By default, for scripts it's footer
Meta::addScript('script.js', 'http://site.com/script.js', [], 'custom_placement');
//
Register a custom tag Our package has a lot of ways of extending. One of them is creating new tags. You are able to create a new class and share it with friends or with the laravel community. You can also create a new pull request if you think that your awesome tag is really useful.
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 '
'
}
}
Meta::addTag('facebook.pixel', new FacebookPixelTag('42b3h23-34234'));
//
Register tags from TagsCollection
$tags = new \Butschster\Head\MetaTags\TagsCollection([
...
]);
Meta::registerTags($tags);
// You can specify the placement
Meta::registerTags($tags, 'footer');Get a tag by name
Meta::getTag('author');Remove a tag by name
Meta::removeTag('author');Add a meta tag
Meta::addMeta('author', [
'content' => 'butschster',
]);
// Add the CSRF-token tag
Meta::addCsrfToken();
// Remove all tags
Meta::reset();Include required packages
Meta::includePackages('jquery', 'vuejs');
Meta::includePackages(['jquery', 'vuejs']);
Will load registered packages with names jquery and vuejs and append tags from there to MetaRegister a new package and register all tags from this package
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);Replace package with the same name When you replace package with a new one, old tags will be removed
$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暂无开放 Issues,或尚未同步最近议题。