This is a simple and extensible package for improving SEO via meta tags, such as OpenGraph tags.
By default, it uses and OpenGraph tags. It also ships with a Twitter extension. You're, of course, free to write your own extensions as needed.
Features:
Setting SEO tags from PHP
Setting SEO tags from Blade
Integration with Flipp, to automatically generate cover images
Custom extension support
Expressive & simple API
Customizable views
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
Installation
composer require archtechx/laravel-seo
And add the following line to your layout file's tag:
<x-seo::meta />
Usage
The package can be used from any PHP code, or specifically from Blade using the @seo directive.
PHP
Use the seo() helper to retrieve the SeoManager instance, on which you can call the following methods:
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').
Twitter
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')
Defaults
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 ...');
Extra tags
To add more tags to the document head, you can use the tag() and rawTag() methods:
');
seo()->rawTag('fb_url', ''); // Keyed, allows overrides later on
">
seo()->tag('fb:image', asset('foo'));
seo()->rawTag('');
seo()->rawTag('fb_url', ''); // Keyed, allows overrides later on
Canonical URL
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).
You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta with | ArchTech when it has a non-default value.
To do that, simply add the modify argument to the method calls like this:
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.
Flipp integration
First, you need to add your Flipp API keys:
Add your API key to the FLIPP_KEY environment variable. You can get the key here.
Go to config/services.php and add:
'flipp' => [
'key' => env('FLIPP_KEY'),
],
Then, register your templates, for example in AppServiceProvider:
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).
Views
You can publish the Blade views by running php artisan vendor:publish --tag=seo-views.
Extensions
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:
">
"facebook-title" content="@seo('facebook.foo')">
Once your view is created, register the extension:
seo()->extension('facebook', view: 'my-component');
// The extension will use
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);
Development
Run all checks locally:
./check
Code style will be automatically fixed by php-cs-fixer.
The @seo blade tag can set e.g. the description like this @seo(['description' => 'foo']). But I didn't find any way to do the same for the social image? The following call (which I expected to work) resulted in an error:
ArchTech\SEO\SEOManager::set(): Argument #2 ($value) must be of type Closure|string|null, array given, called in /Users/tpetry/Documents/laravel-seo/src/SEOManager.php on line 95
@seo(['flipp' => ['blog', ['title' => 'Lorem Ipsum', 'description' => 'Lorem Ipsum, Lorem Ipsum']]])
```php
Is it not supported to set the social image with the `@seo` tag without returning any value or did I just miss the correct call?
I've updated the doc blocks within SEOManager.php as the current ones trigger an error on IDEs such as PHPStorm. The current docs make it seem that only ($title) is allowed, however the exact code from the package documentation show default and modifier, which result in IDE complaining it doesn't exist.
This PR has fixed my original issue, and also doesn't affect usage without named arguments.
Additionally to the twitter:site meta tag, this Pull Request adds the creator one to indicate who is responsible for the article instead of the whole site.
How come when using the <x-seo::meta /> tag, it inputs the HTML all strange? It adds tabs randomly, adds new lines randomly, and joins some things together on the same line. Why not just input everything one line after eachother?
Controller Code:
seo()
->title('News & Updates - MySite')
->description('Find out the latest')
->site('MySite')
->twitter();
Actual HTML output:
<title>News & Updates - MySite</title>
<meta property="og:title" content="News & Updates - MySite" />
<meta property="og:description" content="Find out the latest" />
<meta name="description" content="Find out the latest" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="MySite">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="MySite"> <meta name="twitter:title" content="News & Updates - MySite"> <meta name="twitter:description" content="Find out the latest">
When using raw tags (with the keys), it only adds additional HTML entries into the header, I know the concept behind it is to override as we spoke about previously. I personally use this for favicons, where I change it for viewing pages with user uploaded avatars.
When diving further, your tests (as found https://github.com/archtechx/laravel-seo/blob/master/tests/Pest/ManagerTest.php#L102) seem to reference the wrong value. Shouldn't this be checking for the newer entry, rather than the original abc?
As discussed in #23 Previewify data keys need a prefix to work correctly. I changed the implementation to your recommendation of automatically prefixing the keys.
opened by tpetry 0
Releases(v0.5.1)
v0.5.1(Jul 5, 2022)
What's Changed
Add previewify prefix by @tpetry in https://github.com/archtechx/laravel-seo/pull/24
Full Changelog: https://github.com/archtechx/laravel-seo/compare/v0.5.0...v0.5.1
Laravel Magic provides Abstract Controller, Model, generic Request, Traits, Exceptions and various middlewares in order to generate very easily and quickly API resources from scratch.
Laravel 8 and 9 package to manage URL redirections inside your Laravel application using different data sources. It allows a better SEO support for your Laravel site.