A SEO package made for maximum customization and flexibility.
composer require romanzipp/laravel-seo
Copy configuration to config folder:
$ php artisan vendor:publish --provider="romanzipp\Seo\Providers\SeoServiceProvider"
use romanzipp\Seo\Facades\Seo;
use romanzipp\Seo\Services\SeoService;
class IndexController
{
public function index(Request $request, SeoService $seo)
{
$seo = seo();
$seo = app(SeoService::class);
$seo = Seo::make();
}
}{{ seo()->render() }}This package offers many ways of adding new elements (Structs) to your <head>.
- Add commonly used structs via shorthand setters like
seo()->title('...'),seo()->meta('...') - Manually add single structs via the
seo()->add()methods - Specify an array of contents via
seo()->addFromArray()
Take a look at the structs documentation or example app for more detailed usage.
seo()->title('Laravel');<title>Laravel</title>
<meta property="og:title" content="Laravel" />
<meta name="twitter:title" content="Laravel" />seo()->description('Catchy marketing headline');<meta name="description" content="Catchy marketing headline" />
<meta property="og:description" content="Catchy marketing headline" />
<meta name="twitter:description" content="Catchy marketing headline" />seo()->csrfToken();<meta name="csrf-token" content="a7588c617ea5d8833374d8eb3752bcc4071" />seo()->charset();
seo()->viewport();<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />seo()->twitter('card', 'summary');
seo()->twitter('creator', '@romanzipp');<meta name="twitter:card" content="summary" />
<meta name="twitter:creator" content="@romanzipp" />seo()->og('site_name', 'Laravel');
seo()->og('locale', 'de_DE');<meta name="og:site_name" content="Laravel" />
<meta name="og:locale" content="de_DE" />seo()->meta('copyright', 'Roman Zipp');<meta name="copyright" content="Roman Zipp" />For more information see the structs documentation.
You can add structs to different sections by calling the section('foo') method on the SeoService instance or passing it as the first attribute to the seo('foo') helper method.
Sections allow you to create certain namespaces for Structs which can be used in many different ways: Distinct between "frontend" and "admin" page sections or "head" and "body" view sections.
// This struct will be added to the "default" section
seo()->twitter('card', 'summary');
// This struct will be added to the "secondary" section
seo()->section('secondary')->twitter('card', 'image');
// This struct will be also added to the "default" section since the section() method changes are not persistent
seo()->twitter('card', 'summary');This will render all structs added to the "default" section.
{{ seo()->render() }}This will render all structs added to the "secondary" section.
{{ seo()->section('secondary')->render() }}Of course, you can also pass the section as parameter to the helper function.
{{ seo('secondary')->render() }}You can include your mix-manifest.json file generated by Laravel-Mix to automatically add preload/prefetch link elements to your document head.
seo()
->mix()
->load();mix-manifest.json
{
"/js/app.js": "/js/app.js?id=123456789",
"/css/app.css": "/css/app.css?id=123456789"
}document <head>
<link rel="prefetch" href="/js/app.js?id=123456789" />
<link rel="prefetch" href="/css/app.css?id=123456789" />Take a look at the SEO Laravel-Mix integration docs for further usage.
use romanzipp\Seo\Conductors\Types\ManifestAsset;
seo()
->mix()
->map(static function(ManifestAsset $asset): ?ManifestAsset {
if (strpos($asset->path, 'admin') !== false) {
return null;
}
$asset->url = "http://localhost/{$asset->url}";
return $asset;
})
->load(public_path('custom-manifest.json'));This package features a basic integration for Spaties Schema.org package to generate ld+json scripts. Added Schema types render with the packages structs.
use Spatie\SchemaOrg\Schema;
seo()->addSchema(
Schema::localBusiness()->name('Spatie')
);use Spatie\SchemaOrg\Schema;
seo()->setSchemes([
Schema::localBusiness()->name('Spatie'),
Schema::airline()->name('Spatie'),
]);Take a look at the Schema.org package Docs.
| Code | Rendered HTML |
|---|---|
| Shorthand Setters | |
seo()->title('Laravel') |
<title>Laravel</title> |
seo()->description('Laravel') |
<meta name="description" content="Laravel" /> |
seo()->meta('author', 'Roman Zipp') |
<meta name="author" content="Roman Zipp" /> |
seo()->twitter('card', 'summary') |
<meta name="twitter:card" content="summary" /> |
seo()->og('site_name', 'Laravel') |
<meta name="og:site_name" content="Laravel" /> |
seo()->charset() |
<meta charset="utf-8" /> |
seo()->viewport() |
<meta name="viewport" content="width=device-width, ..." /> |
seo()->csrfToken() |
<meta name="csrf-token" content="..." /> |
| Adding Structs | |
seo()->add(...) |
<meta name="foo" /> |
seo()->addMany([...]) |
<meta name="foo" /> |
seo()->addIf(true, ...) |
<meta name="foo" /> |
| Various | |
seo()->mix() |
|
seo()->hook() |
|
seo()->render() |
./vendor/bin/phpunit