From 3f45aae5d66016b4a9b29ac7eed5cb01b47f4202 Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Sun, 25 Aug 2024 17:10:57 +0200 Subject: [PATCH] add defaults --- README.md | 221 +++++++++++++++++++++++++++++------------ src/Schemas/Schema.php | 25 +++-- src/SeoImage.php | 38 +++++++ src/SeoManager.php | 46 +++++++-- 4 files changed, 254 insertions(+), 76 deletions(-) create mode 100644 src/SeoImage.php diff --git a/README.md b/README.md index a319219..fcc2a88 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ![laravel-seo](https://repository-images.githubusercontent.com/845966143/6ff7437c-852d-41eb-8b2f-927551506a13) -This package offers an extremely flexible and advanced way to manage all of your SEO tags. Unlike other packages that focus on the most basic and common tags, this one implements all the protocols. +This package offers an extremely flexible and advanced way to manage all of your SEO tags. With this package, you will be able to implement: @@ -36,63 +36,135 @@ This is the content of the published config file: ```php return [ - /* - |-------------------------------------------------------------------------- - | Default Title - |-------------------------------------------------------------------------- - | - | This is the default value used for , "og:title", "twitter:title" - | - */ - 'title' => env('APP_NAME', 'Laravel'), - - /* - |-------------------------------------------------------------------------- - | Default Description - |-------------------------------------------------------------------------- - | - | This is the default value used for <meta name="description">, <meta property="og:description">, <meta name="twitter:description"> - | - */ - 'description' => null, - - /* - |-------------------------------------------------------------------------- - | Default Image path - |-------------------------------------------------------------------------- - | - | This is the default value used for <meta property="og:image">, <meta name="twitter:image"> - | You can use relative path like "/opengraph.png" or url like "https://example.com/opengraph.png" - | - */ - 'image' => null, - - /* - |-------------------------------------------------------------------------- - | Default Robots - |-------------------------------------------------------------------------- - | - | This is the default value used for <meta name="robots"> - | See Google documentation here: https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag?hl=fr#directives - | - */ - 'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1', - - /* - |-------------------------------------------------------------------------- - | Default Sitemap path - |-------------------------------------------------------------------------- - | - | This is the default value used for <link rel="sitemap"> - | You can use relative path like "/sitemap.xml" or url like "https://example.com/sitemap.xml" - | - */ - 'sitemap' => null, + 'defaults' => [ + /* + |-------------------------------------------------------------------------- + | Default Title + |-------------------------------------------------------------------------- + | + | This is the default value used for <title>, "og:title", "twitter:title" + | + */ + 'title' => env('APP_NAME', 'Laravel'), + + /* + |-------------------------------------------------------------------------- + | Default Description + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta name="description">, <meta property="og:description">, <meta name="twitter:description"> + | + */ + 'description' => null, + + /* + |-------------------------------------------------------------------------- + | Default Keywords + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta name="keywords"> + | Type supported: string or array of strings + | + */ + 'keywords' => null, + + /* + |-------------------------------------------------------------------------- + | Default Image path + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta property="og:image">, <meta name="twitter:image"> + | You can use relative path like "/opengraph.png" or url like "https://example.com/opengraph.png" + | + */ + 'image' => null, + + /* + |-------------------------------------------------------------------------- + | Default Robots + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta name="robots"> + | See Google documentation here: https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag?hl=fr#directives + | + */ + 'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1', + + /* + |-------------------------------------------------------------------------- + | Default Sitemap path + |-------------------------------------------------------------------------- + | + | This is the default value used for <link rel="sitemap"> + | You can use relative path like "/sitemap.xml" or url like "https://example.com/sitemap.xml" + | + */ + 'sitemap' => null, + ], + + /** + * @see https://ogp.me/ + */ + 'opengraph' => [ + /* + |-------------------------------------------------------------------------- + | Default Site Name + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta property="og:site_name" /> + | If null: config('app.name') is used. + | + */ + 'site_name' => null, + + /* + |-------------------------------------------------------------------------- + | Default Determiner + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta property="og:determiner" /> + | Possible values are: a, an, the, "", auto + | + */ + 'determiner' => '', + ], + + /** + * @see https://developer.x.com/en/docs/x-for-websites/cards/overview/abouts-cards + */ + 'twitter' => [ + /* + |-------------------------------------------------------------------------- + | Default Twitter username + |-------------------------------------------------------------------------- + | + | This is the default value used for <meta name="twitter:site" /> + | Example: @X + | + */ + 'site' => null, + ], + + /** + * @see https://schema.org/WebPage + */ + 'schema' => [ + /* + |-------------------------------------------------------------------------- + | Default WebPage schema + |-------------------------------------------------------------------------- + | + | This is the default value used for the schema WebPage + | @see https://schema.org/WebPage for all available properties + | + */ + 'defaults' => [], + ], ]; ``` -## Usage +## Introduction You can display all the SEO tags in your view simply by calling the `seo` function like this: @@ -102,29 +174,52 @@ You can display all the SEO tags in your view simply by calling the `seo` functi </head> ``` -This function accepts different kinds of arguments, allowing you to take full control over your SEO. - -### Basic SEO +This will render all the default tags: + +```html +<title>Home + + + + + + + + + + + + + +``` -The simplest way to define your SEO tags is with `Elegantly\Seo\SeoData::class`. -This class provides a unified representation of the most common SEO tags (Open Graph, Twitter, etc.). -It will also use the defaults defined in your config. +### Basic Usage #### From a Controller -Define a `SeoData` object and pass it to the view: +Most of the time, your will want to define you seo tags from a controller ```php namespace App\Http\Controllers; -use Elegantly\Seo\SeoData; +use \Elegantly\Seo\SeoManager; class HomeController extends Controller { function __invoke() { return view('home', [ - 'seo' => new SeoData( + 'seo' => SeoManager::default( title: "Homepage", ) ]); diff --git a/src/Schemas/Schema.php b/src/Schemas/Schema.php index eef5185..97978ab 100644 --- a/src/Schemas/Schema.php +++ b/src/Schemas/Schema.php @@ -23,18 +23,29 @@ public function toTags(): SeoTags ]); } - public static function default(): self - { - $schema = new self; - - return $schema->merge([ + public static function default( + ?string $title = null, + ?string $url = null, + ?string $description = null, + ?string $image = null, + ): self { + $schema = new self([ '@context' => 'https://schema.org', '@type' => 'WebPage', 'name' => __(config('seo.defaults.title')), 'description' => __(config('seo.defaults.description')), 'image' => config('seo.defaults.image.url'), 'url' => Request::url(), - ...config('seo.schema.defaults', []), - ])->filter(); + ]); + + return $schema + ->merge(config('seo.schema.defaults', [])) + ->merge(array_filter([ + 'name' => $title, + 'description' => $description, + 'image' => $image, + 'url' => $url, + ])) + ->filter(); } } diff --git a/src/SeoImage.php b/src/SeoImage.php new file mode 100644 index 0000000..0a3eca7 --- /dev/null +++ b/src/SeoImage.php @@ -0,0 +1,38 @@ +url, + secure_url: $this->secure_url, + type: $this->type, + width: $this->width, + height: $this->height, + alt: $this->alt, + ); + } + + public function toTwitter(): TwitterImage + { + return new TwitterImage( + url: $this->secure_url ?? $this->url, + alt: $this->alt + ); + } +} diff --git a/src/SeoManager.php b/src/SeoManager.php index e516d43..47fa8a4 100644 --- a/src/SeoManager.php +++ b/src/SeoManager.php @@ -5,6 +5,7 @@ use Elegantly\Seo\Contracts\Taggable; use Elegantly\Seo\OpenGraph\OpenGraph; use Elegantly\Seo\Schemas\Schema; +use Elegantly\Seo\Standard\Alternate; use Elegantly\Seo\Standard\StandardData; use Elegantly\Seo\Twitter\Cards\Card; use Elegantly\Seo\Twitter\Cards\Summary; @@ -29,13 +30,46 @@ public function current(): static return $this; } - public static function default(): self - { + /** + * @param null|string|string[] $keywords + * @param null|Alternate[] $alternates + */ + public static function default( + ?string $title = null, + ?string $url = null, + ?string $description = null, + null|string|array $keywords = null, + ?SeoImage $image = null, + ?string $robots = null, + ?string $sitemap = null, + ?array $alternates = null, + ): self { return new self( - standard: StandardData::default(), - opengraph: OpenGraph::default(), - twitter: Summary::default(), - schemas: [Schema::default()], + standard: StandardData::default( + $title, + $url, + $description, + $keywords, + $robots, + $sitemap, + $alternates + ), + opengraph: OpenGraph::default( + title: $title, + url: $url, + image: $image?->toOpenGraph(), + ), + twitter: Summary::default( + title: $title, + description: $description, + image: $image?->toTwitter(), + ), + schemas: [Schema::default( + title: $title, + url: $url, + description: $description, + image: $image?->secure_url ?? $image?->url, + )], ); }