bayrameker/laravelseo

dev-main 2022-06-23 05:49 UTC

This package is not auto-updated.

Last update: 2024-09-30 02:26:11 UTC


README

特性:

  • 从PHP中设置SEO标签
  • 从Blade中设置SEO标签
  • 与Flipp集成以自动创建封面图片
  • 特殊扩展支持
  • 强大且简单的API
  • 可自定义的外观

示例用法

seo()
    ->title($post->title)
    ->description($post->excerpt)
    ->twitter()
    ->flipp('blog')

安装

composer require bayrameker/laravelseo

将以下行添加到您的Blade文件中的 <head> 标签

<x-seo::meta />

使用方法

此包可以从任何PHP代码中或通过使用特殊的 @seo 指令从Blade中调用。

PHP

使用 seo() 助手函数获取SeoManager示例,您可以调用以下方法

现有方法

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)

示例用法

seo()->title('foo')->description('bar')

Blade文件

您可以使用 @seo 指令从Blade中调用这些方法

@seo('title') // title değerini kullan
@seo('title', 'foo') // title değer tanımla ve kullan
@seo(['title' => 'foo']) // Başlığı önceden tanımla

Twitter

seo()->twitter();
seo()->twitterTitle('About us')

favicon

默认情况下,不会添加任何favicon链接。您可以使用以下方式自行添加

seo()->favicon();

创建favicon

创建favicon的代码

php artisan seo:generate-favicons public/path-to/logo.png

如果没有提供任何路径参数,Artisan控制台将返回 'public/assets/logo.png' 位置。

我们将创建32x32像素的 "public/favicon.ico" 和 "public/favicon.png" 图标。在大多数情况下,这将是足够的。

请记住,您需要安装 imagick PHP 扩展和 intervention/image

默认值

要配置默认值,请使用 "default" 依赖变量调用方法

seo()
    ->title(default: 'Bayram Eker — Kişisel Website')
    ->description(default: 'Biz yazılım geliştiricisiyiz ...');

额外标签

要添加更多标签,可以使用 "tag()" 和 "rawTag()" 方法

seo()->tag('fb:image', asset('foo'));
seo()->rawTag('<meta property="fb:url" content="bar" />');
seo()->rawTag('fb_url', '<meta property="fb:url" content="bar" />'); 

标准URL

要启用 "og:url" 和规则URL "链接" 标签,请搜索以下内容

seo()->withUrl();

这将从包的 request()->url() (不带查询字符串的有效URL) 元素中读取。

如果您想更改URL,请使用 seo()->url()

seo()->url(route('products.show', $this->product));

值更改

您可能希望在添加到模板之前更改某些值。例如,您可能想将标题meta的末尾添加一个标题。

为此,您只需向方法调用添加 'modify' 参数即可

seo()->title(modify: fn (string $title) => $title . ' | Bayram Eker');

当然,您可以将其与默认值结合使用

seo()->title(
    default: 'BayramEker — Web Geliştirici',
    modify: fn (string $title) => $title . ' | Laravel Developer'
);

Flipp集成

首先,您需要添加Flipp API密钥

  1. 将API密钥添加到 "FLIPP_KEY" 环境变量中。您可以从 [这里]->(https://useflipp.com/settings/profile/api) 获取密钥。

  2. 转到 config/services.php 并添加以下内容

    'flipp' => [
        'key' => env('FLIPP_KEY'),
    ],

将模板添加到 AppServiceProvider

seo()->flipp('blog', 'v8ywdwho3bso');
seo()->flipp('page', 'egssigeabtm7');

然后,您可以通过调用 seo()->flipp() 来使用模板

seo()->flipp('blog', ['title' => 'Foo', 'content' => 'bar'])`

如果没有提供任何数据,则方法将使用当前SEO配置中的 "标题" 和 "描述"。

seo()->title($post->title);
seo()->description($post->excerpt);
seo()->flipp('blog');

此外,'flipp()' 方法还会返回一个与图片相关的URL,允许您在其他地方(如博客封面图片)使用。

<img alt="@seo('title')" src="@seo('flipp', 'blog')">

示例

服务提供商

Services Provider boot() 方法中的默认配置

seo()
    ->site('Bayram Eker — Laravel Developer')
    ->title(
        default: 'BayramEker — Biz yazılım geliştiricisiyiz',
        modify: fn (string $title) => $title . ' | Laravel Developer'
    )
    ->description(default: 'yazılım geliştiricisiyiz ...')
    ->image(default: fn () => asset('header.png'))
    ->flipp('blog', 'o1vhcg5npgfu')
    ->twitterSite('archtechx');

控制器

控制器SEO元数据配置示例。

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));
}

视图

此示例使用Blade视图,该视图根据传递给视图的值设置SEO配置。

@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>

自定义

此包完全开源,可以通过更改外观(通过替换现有模板)或通过开发扩展(添加更多模板)进行自定义。

显示

通过运行 php artisan satıcısı:yayın --tag=seo-views 命令来发布Blade视图。

扩展

要使用特殊扩展,请使用所需的元标签创建一个Blade 组件。组件应使用 {{ seo()->get('foo') }}@seo('foo') 来读取数据。

例如

<meta name="facebook-title" content="@seo('facebook.foo')">

在创建视图后保存扩展

seo()->extension('facebook', view: 'my-component');
//  <x-my-component>

为扩展设置数据时(以我们的情况为例 facebook),以驼峰式调用扩展名称作为前缀,或者使用 ->set() 方法。

seo()->facebookFoo('bar');
seo()->facebookTitle('Hakkımızda');
seo()->set('facebook.description', 'Biz bir web geliştirme ...');
seo(['facebook.description' => 'Biz bir web geliştirme ...']);

要禁用扩展,请在 "extension()" 调用的第二个参数中将值设置为 false。

seo()->uzantı('facebook', false);

开发

在本地运行所有检查

./check

代码风格将由 php-cs-fixer 自动修复。