Package Data | |
---|---|
Maintainer Username: | stancl |
Maintainer Contact: | samuel@archte.ch (Samuel Štancl) |
Package Create Date: | 2021-05-23 |
Package Last Update: | 2024-11-22 |
Home Page: | https://archte.ch/blog/introducing-laravel-seo |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-18 03:00:57 |
Package Statistics | |
---|---|
Total Downloads: | 177,286 |
Monthly Downloads: | 10,500 |
Daily Downloads: | 547 |
Total Stars: | 300 |
Total Watchers: | 6 |
Total Forks: | 31 |
Total Open Issues: | 4 |
This is a simple and extensible package for improving SEO via meta tags, such as OpenGraph tags.
By default, it uses <title>
and OpenGraph tags. It also ships with a Twitter extension. You're, of course, free to write your own extensions as needed.
Features:
Example usage:
seo()
->title($post->title)
->description($post->excerpt)
->twitter()
->flipp('blog')
// Adds OpenGraph tags
// Adds Twitter card tags
// Generates social image using Flipp and sets it as the cover photo
composer require archtechx/laravel-seo
And add the following line to your layout file's <head>
tag:
<x-seo::meta />
The package can be used from any PHP code, or specifically from Blade using the @seo
directive.
Use the seo()
helper to retrieve the SeoManager instance, on which you can call the following methods:
Available methods:
site(string $site)
url(string $url)
title(string $title)
description(string $description)
image(string $url)
twitterCreator(string $username)
twitterSite(string $username)
twitterTitle(string $title)
twitterDescription(string $description)
twitterImage(string $url)
Example usage:
seo()->title('foo')->description('bar')
You can use the @seo
directive to call the methods from Blade:
@seo('title') // Echoes the title
@seo('title', 'foo') // Sets the title & echoes it
@seo(['title' => 'foo']) // Sets the title without echoing it
In general, you'll want to use @seo(['title' => 'foo'])
at the start of a view — to set the values — and @seo('title')
inside the view if you wish to fetch the value.
That is, if you'll use the helpers in Blade at all. Some apps will only use the PHP helper.
For Twitter, use the twitter.author
format, e.g. @seo('twitter.author')
.
By default, no Twitter tags will be included. If you manually enable the extension by calling:
seo()->twitter();
in a service provider for example, the extension will be enabled.
Once it's enabled, it will copy all default (OpenGraph) values and use them for the Twitter card schema.
When a value is set specifically for Twitter, it will be prioritized over the general fallback values.
seo()->twitterTitle('About us')
By default, no favicon links will be included. You can manually enable the extension by calling:
seo()->favicon();
To generate favicon, run:
php artisan seo:generate-favicons public/path-to/logo.png
from the artisan console. If no path argument is given we'll fallback to public/assets/logo.png
.
We'll generate a 32x32px public/favicon.ico
& public/favicon.png
icon. This should be sufficient for most cases.
Please keep in mind that you need to install the imagick php extension and intervention/image composer package.
To configure default values, call the methods with the default
argument:
seo()
->title(default: 'ArchTech — Meticulously architected web applications')
->description(default: 'We are a web development agency that ...');
To add more tags to the document head, you can use the tag()
and rawTag()
methods:
seo()->tag('fb:image', asset('foo'));
seo()->rawTag('<meta property="fb:url" content="bar" />');
seo()->rawTag('fb_url', '<meta property="fb:url" content="bar" />'); // Keyed, allows overrides later on
To enable the og:url
and canonical URL link
tags, call:
seo()->withUrl();
This will make the package read from request()->url()
(the current URL without the query string).
If you wish to change the URL, call seo()->url()
:
seo()->url(route('products.show', $this->product));
You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta <title>
with | ArchTech
when it has a non-default value.
To do that, simply add the modify
argument to the method calls like this:
seo()->title(modify: fn (string $title) => $title . ' | ArchTech');
You can, of course, combine these with the defaults:
seo()->title(
default: 'ArchTech — Meticulously architected web applications',
modify: fn (string $title) => $title . ' | ArchTech'
);
Which will make the package use the default if no title is provided, and if a title is provided using e.g. seo()->title('Blog')
, it will be modified right before being inserted into the template.
First, you need to add your Flipp API keys:
FLIPP_KEY
environment variable. You can get the key here.config/services.php
and add:
'flipp' => [
'key' => env('FLIPP_KEY'),
],
Then, register your templates, for example in AppServiceProvider
:
seo()->flipp('blog', 'v8ywdwho3bso');
seo()->flipp('page', 'egssigeabtm7');
After that, you can use the templates by calling seo()->flipp()
like this:
seo()->flipp('blog', ['title' => 'Foo', 'content' => 'bar'])`
The call will set the generated image as the OpenGraph and Twitter card images. The generated URLs are signed.
If no data array is provided, the method will use the title
and description
from the current SEO config:
seo()->title($post->title);
seo()->description($post->excerpt);
seo()->flipp('blog');
The flipp()
method also returns a signed URL to the image, which lets you use it in other places, such as blog cover images.
<img alt="@seo('title')" src="@seo('flipp', 'blog')">
First, you need to add your Previewify API keys:
PREVIEWIFY_KEY
environment variable. You can get the key here.config/services.php
and add:
'previewify' => [
'key' => env('PREVIEWIFY_KEY'),
],
Then, register your templates, for example in AppServiceProvider
:
seo()->previewify('blog', 24);
seo()->previewify('page', 83);
After that, you can use the templates by calling seo()->previewify()
like this:
seo()->previewify('blog', ['title' => 'Foo', 'content' => 'bar'])`
The call will set the generated image as the OpenGraph and Twitter card images. The generated URLs are signed.
If no data array is provided, the method will use the title
and description
from the current SEO config:
seo()->title($post->title);
seo()->description($post->excerpt);
seo()->previewify('blog');
The previewify()
method also returns a signed URL to the image, which lets you use it in other places, such as blog cover images.
<img alt="@seo('title')" src="@seo('previewify', 'blog')">
Note The
previewify:
prefix will be automatically prepended to all provided data keys.
This example sets the default state in a service provider's boot()
method:
seo()
->site('ArchTech — Meticulously architected web applications')
->title(
default: 'ArchTech — Meticulously architected web applications',
modify: fn (string $title) => $title . ' | ArchTech'
)
->description(default: 'We are a development agency ...')
->image(default: fn () => asset('header.png'))
->flipp('blog', 'o1vhcg5npgfu')
->twitterSite('archtechx');
This example configures SEO metadata from a controller.
public function show(Post $post)
{
seo()
->title($post->title)
->description(Str::limit($post->content, 50))
->flipp('blog', ['title' => $page->title, 'content' => $page->excerpt]);
return view('blog.show', compact($post));
}
This example uses a Blade view that sets global SEO config using the values that are passed to the view.
@seo(['title' => $page->name])
@seo(['description' => $page->excerpt])
@seo(['flipp' => 'content'])
<h1>{{ $page->title }}</h1>
<p>{{ $page->excerpt }}</p>
<p class="prose">
{{ $page->body }}
</p>
This package is completely flexible, and can be customized either by having its views modified (to change the existing templates), or by you developing an extension (to add more templates).
You can publish the Blade views by running php artisan vendor:publish --tag=seo-views
.
To use a custom extension, create a Blade component with the desired meta tags. The component should read data using {{ seo()->get('foo') }}
or @seo('foo')
.
For example:
<meta name="facebook-title" content="@seo('facebook.foo')">
Once your view is created, register the extension:
seo()->extension('facebook', view: 'my-component');
// The extension will use <x-my-component>
To set data for an extension (in our case facebook
), simply prefix calls with the extension name in camelCase, or use the ->set()
method:
seo()->facebookFoo('bar');
seo()->facebookTitle('About us');
seo()->set('facebook.description', 'We are a web development agency that ...');
seo(['facebook.description' => 'We are a web development agency that ...']);
To disable an extension, set the second argument in the extension()
call to false:
seo()->extension('facebook', false);
Run all checks locally:
./check
Code style will be automatically fixed by php-cs-fixer.