An SEO configuration library for Laravel applications.
An SEO configuration library for Laravel applications.
The Honeystone SEO package makes configuring SEO metadata from anywhere within your Laravel application a breeze.
Included are metadata generators for general metadata, X (Formally Twitter) Cards, Open Graph and JSON-LD Schema.
This package was designed with extensibility in mind, so your own custom metadata generators can also be added with ease.
We are committed to delivering high-quality open source packages maintained by the team at Honeystone. If you would like to support our efforts, simply use our packages, recommend them and contribute.
If you need any help with your project, or require any custom development, please get in touch.
composer require honeystone/laravel-seoPublish the configuration file with:
php artisan vendor:publish --tag=honeystone-seo-configThe package provides a helper function, seo(), and some Blade directives, @metadata and @openGraphPrefix. You
can also typehint the Honeystone\Seo\MetadataDirector if you prefer to use dependency injection.
Setting metadata is a simple as chaining methods:
seo()
->title('A fantastic blog post', 'My Awesome Website!')
->description('Theres really a lot of great stuff in here...')
->images(
'https://mywebsite.com/images/blog-1/cover-image.webp',
'https://mywebsite.com/images/blog-1/another-image.webp',
);Once you've set your metadata, you can render it using:
seo()->generate();Alternatively, you can also use the @metadata Blade directive.
The rendered result will look something like this:
A fantastic blog post - My Awesome Website!
Every generator now implements the Honeystone\Seo\Contracts\ExportsArrayMetadata contract, which means the director
can provide a structured payload for each generator:
$seo = seo()->toArray(); // ['meta' => [...], 'twitter' => [...], ...]
$metaOnly = seo()->toArray('meta'); // limit to a specific generatorThis is particularly useful when sharing SEO data with Inertia.js:
return Inertia::render('Pages/Show', [
'page' => $page,
'seo' => seo()->toArray(),
]);On the client you can decide whether to render the tags yourself, hydrate a preview component, or simply inspect the payload for analytics/debugging purposes.
If you are using the bundled GenerateInertiaMetadata middleware, the structured payload is automatically shared as
$page.props.seoPayload (alongside the rendered HTML string that remains available under $page.props.seo for legacy
integrations).
When using Inertia with React, you can render the structured SEO payload directly in the head using the bundled React
component at resources/js/inertia/react/SeoHead.tsx.
\Honeystone\Seo\Http\Middleware\GenerateInertiaMetadataReact::class in your
Inertia middleware stack so $page.props.seoPayload is shared:// app/Http/Middleware/HandleInertiaRequests.php
protected $middleware = [
// ...
\Honeystone\Seo\Http\Middleware\GenerateInertiaMetadataReact::class,
];// resources/js/Layouts/AppLayout.tsx
import SeoHead from '@/inertia/react/SeoHead';
export default function AppLayout({ children }) {
return (
<>
{children}
);
}seoPayload from usePage() and outputs meta, Open Graph, Twitter and JSON-LD tags. Pass
forceNoIndex if you need to override robots for specific responses.When using this method, Inertia itself will attempt to replace the meta tags for you so you will need to remove the @metadata blade directive from your layouts.
Values provided to default methods will automatically propagate to all configured metadata generators.
The following default methods are available:
…The full baseline looks like this:
A fantastic blog post
For your homepage you'll probably want to disable the title template:
seo()->title('My Awesome Website!', template: false);The meta methods are provided by the Honeystone\Seo\Generators\MetaGenerator class.
Here's the full list:
seo()
->metaTitle('A fantastic blog post')
->metaTitleTemplate(' {title} ')
->metaDescription('Theres really a lot of great stuff in here...')
->metaKeywords('foo', 'bar', 'baz')
->metaCanonical('https://mywebsite.com/blog/a-fantastic-blog-post')
->metaCanonicalEnabled(true)
->metaRobots('', '', '');All of these are provided by the default methods and propagate through to the meta generator.
If you only want to render the meta generator, use seo()->generate('meta') or @metadata('meta')
The meta methods are provided by the Honeystone\Seo\Generators\TwitterGenerator class.
Here's the full list:
seo()
->twitterEnabled(true) //enabled by default, see config
->twitterSite('@MyWebsite')
->twitterCreator('@MyTwitter')
->twitterTitle('A fantastic blog post') //defaults to title()
->twitterDescription('Theres really a lot of great stuff in here...') //defaults to description()
->twitterImage('https://mywebsite.com/images/blog-1/cover-image.webp'); //defaults to the first in images()The meta methods are provided by the Honeystone\Seo\Generators\TwitterGenerator class.
Here's the full list:
…You can also use the following non-vertical supported types:
…You can provide more data for images, audio and videos using their respective properties classes:
…Here's an example using ArticleProperties and ImageProperties:
To set the prefix, you can use the @openGraphPrefix Blade directive or seo()->openGraphPrefix() like so:
...
The meta methods are provided by the Honeystone\Seo\Generators\JsonLdGenerator class.
Here's the full list:
…And the output:
But Wait, There's More!
Rather than reinventing the wheel, this package has support for the incredible
spatie/schema-org package. You can use the jsonLdImport() method to import an
exising schema, or build your schema using the fluent interface.
//graph
seo()->jsonLdGraph()
->organization('honeystone')
->name('Honeystone')
->legalName('Honeystone Consulting Ltd.');
//or a MultiTypedEntity
seo()->jsonLdMulti()
->organization('honeystone')
->name('Honeystone')
->legalName('Honeystone Consulting Ltd.');Just don't forget to install the spatie/schema-org package to use this functionality.
It's highly likely you'll be building your graph from many locations around your application, e.g. middleware, controllers, view composers, view components, etc.
This is where expectations come in. Simply specify your expectations, and then ensure the other parts of your application check in. If something failed to check in, an exception will be thrown. Conversely, if something unexpected checked in, an exception will also be thrown.
//perhaps in a controller
seo()
->title('Something awesome')
->jsonLdExpect('featured-tags', 'gallery', 'contact');
//maybe in a view composer or component
seo()
->jsonLdCheckIn('gallery')
->jsonLdGraph()
->imageGallery()
->image([
...
]);You'll be warned immediately if 'featured-tags' or 'contact' fail to check in.
This feature is entirely optional. Just don't set any expectations, or check in, and no exceptions will be thrown.
This package doesn't include any specific functionality for integrating models. Ultimately, you'll always need to map
your model attributes to this package. For example, if your model has a meta_description attribute, you will need to
map it to description, otherwise this package would not know to consume it.
With this in mind, we have a simple pattern that should get you what you need.
Start by adding a new method to your model, and set your metadata using the model's attributes within:
use Honeystone\Seo\MetadataDirector;
use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
public function seo(): MetadataDirector
{
return seo()
->title($this->meta_title)
->description($this->meta_description)
->jsonLdExpect('featured-items');
}
}
Then in your controller, just call the method and chain any additional metadata:
use Illuminate\Contracts\View\View;
class PageController
{
public function __invoke(Page $page): View
{
$page->seo()
->jsonLdCheckIn('featured-items')
->jsonLdGraph()
->itemList()
->name('Featured items')
->itemListElement([
//...
]);
}
}
To create a custom generator, simply implement the Honeystone\Seo\Contracts\MetadataGenerator contract and add it to
your config file in the generators section. You can specify any configuration for your generator here too.
The package includes comments in the generated HTML. If you would like to remove them, you can publish the views and remove the comments from the Blade files:
php artisan vendor:publish --tag=honeystone-seo-viewsPublishing the package views is generally not recommended as these files will not be updated when the package is updated. If it is absolutely necessary to publish the views, we recommend only publishing the files you need to modify.
Here's the full config file:
…A list of changes can be found in the CHANGELOG.md file.
MIT © George Palmer, Honeystone Consulting Ltd
No open issues yet, or sync has not completed.