h1ch4m/seo-helper

v1.0.5 2024-09-07 20:54 UTC

This package is auto-updated.

Last update: 2024-09-07 20:56:48 UTC


README

这是一个用于SEO功能的Laravel包。

安装

您可以通过Composer安装此包

composer require h1ch4m/seo-helper

服务提供者

安装包后,您需要注册服务提供者。将服务提供者添加到您的config/app.php文件中

'providers' => [
    // Other Service Providers...

    H1ch4m\SeoHelper\SeoServiceProvider::class,
],

中间件

该包包含用于处理SEO元数据的中间件。您可以使用此中间件自动将SEO设置应用于您的响应。

注册中间件

要使用中间件,您需要在您的app/Http/Kernel.php文件中注册它。

全局中间件

要将中间件应用于所有路由,请将其添加到$middleware数组中

protected $middleware = [
    // Other middleware...

    \H1ch4m\SeoHelper\Http\Middleware\SeoMiddleware::class,
];

路由中间件

要将中间件应用于特定路由或路由组,请将其添加到$routeMiddleware数组中

protected $routeMiddleware = [
    // Other middleware...

    'seo-helper' => \H1ch4m\SeoHelper\Http\Middleware\SeoMiddleware::class,
];

然后您可以在路由中使用此中间件,如下所示

Route::middleware(['seo-helper'])->group(function () {
    Route::get('/example', ['ExampleController','show']);
});

发布配置

如果您的包包含配置选项,您可以使用Artisan发布配置文件

php artisan vendor:publish --provider="H1ch4m\SeoHelper\SeoServiceProvider"

这将发布配置文件到config/seo-helper.php,您可以在其中根据需要调整设置。

迁移

运行迁移以创建seo-helper

php artisan migrate

使用方法

添加SEO元数据

您可以使用包的API来设置应用程序的SEO元数据。以下是在控制器中使用它的示例

use H1ch4m\SeoHelper\Models\SeoHelper;

public function someMethod()
{
    SeoHelper::insert([
        [
            'name' => 'name',
            'value' => 'author',
            'content' => 'h1ch4m',
            'route' => 'home.index',
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'name' => 'property',
            'value' => 'og:image:width',
            'content' => '256',
            'route' => 'home.index|about.index',
            'created_at' => now(),
            'updated_at' => now(),
        ],
        [
            'name' => 'http-equiv',
            'value' => 'X-UA-Compatible',
            'content' => 'IE=edge',
            'route' => '*', // Wildcard to apply to all routes
            'created_at' => now(),
            'updated_at' => now(),
        ],
    ]);

    // Additional logic...
}

注意

  • 如果您只想在单个路由上应用,请在route列中添加,如果想在多个路由上应用,请在路由之间使用|分隔,如果想在所有路由上应用,请插入*
  • 名称可以是nameproperty或任何您想要的元数据名称
  • 值是元数据名称值
  • 内容是元数据内容

应用中间件

SeoMiddleware将根据您的路由自动处理SEO设置。请确保您已正确配置中间件以应用于所需的路由。

许可证

此包是开源软件,根据MIT许可证授权。