Skip to content

Commit

Permalink
add defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 25, 2024
1 parent 3f1e3c7 commit 3f45aae
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 76 deletions.
221 changes: 158 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -36,63 +36,135 @@ This is the content of the published config file:
```php
return [

/*
|--------------------------------------------------------------------------
| 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 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:

Expand All @@ -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</title>
<meta name="robots" content="'.$robots.'" />
<link
rel="canonical"
href="max-snippet:-1,max-image-preview:large,max-video-preview:-1"
/>
<!-- opengraph -->
<meta property="og:title" content="Laravel" />
<meta property="og:url" content="https://exemple.com" />
<meta property="og:locale" content="en" />
<meta property="og:site_name" content="Laravel" />
<meta property="og:type" content="website" />
<!-- twitter / X -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Laravel" />
<!-- JSON+LD -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebPage",
"name": "Home",
"url": "https://example.com"
}
</script>
```

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",
)
]);
Expand Down
25 changes: 18 additions & 7 deletions src/Schemas/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
38 changes: 38 additions & 0 deletions src/SeoImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Elegantly\Seo;

use Elegantly\Seo\OpenGraph\Image;
use Elegantly\Seo\Twitter\Image as TwitterImage;

class SeoImage
{
public function __construct(
public string $url,
public ?string $secure_url = null,
public ?string $type = null,
public ?string $width = null,
public ?string $height = null,
public ?string $alt = null,
) {}

public function toOpenGraph(): Image
{
return new Image(
url: $this->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
);
}
}
46 changes: 40 additions & 6 deletions src/SeoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
)],
);
}

Expand Down

0 comments on commit 3f45aae

Please sign in to comment.