VRUSION/laravel-sitemappable

laravel-sitemappable

1.7.0 2024-03-13 08:07 UTC

This package is auto-updated.

Last update: 2024-09-13 09:17:38 UTC


README

Latest Version on Packagist Tests Total Downloads

安装

您可以通过composer安装此包

composer require vursion/laravel-sitemappable

如果您使用Laravel >= 5.5,则无需注册服务提供程序。该包将自动注册。安装包后,您可以在config/app.php中的提供者数组中注册服务提供程序

'providers' => [
    ...
    Vursion\LaravelSitemappable\SitemappableServiceProvider::class
],

您需要使用以下命令发布迁移

php artisan vendor:publish --provider="Vursion\LaravelSitemappable\SitemappableServiceProvider" --tag=migrations

您应该使用以下命令发布config/sitemappable.php配置文件

php artisan vendor:publish --provider="Vursion\LaravelSitemappable\SitemappableServiceProvider" --tag=config

这是发布配置文件的内容

return [

    /*
     * This is the name of the table that will be created by the migration and
     * used by the Sitemappable model shipped with this package.
     */
    'db_table_name' => 'sitemap',

    /*
     * The generated XML sitemap is cached to speed up performance.
     */
    'cache' => '60 minutes',

    /*
     * The batch import will loop through this directory and search for models
     * that use the IsSitemappable trait.
     */
    'model_directory' => 'app/Models',

    /*
     * If you're extending the controller, you'll need to specify the new location here.
     */
    'controller' => Vursion\LaravelSitemappable\Http\Controllers\SitemappableController::class,

];

使模型可站点映射

使模型可站点映射的必要步骤是

  • 添加Vursion\LaravelSitemappable\IsSitemappable特质。
  • 定义一个公共方法toSitemappableArray,该方法返回一个包含(本地化)URL(s)的数组。
  • 可选地,在公共方法shouldBeSitemappable中定义模型何时应该是可站点映射的条件。

以下是一个模型示例

use Illuminate\Database\Eloquent\Model;
use Vursion\LaravelSitemappable\IsSitemappable;

class YourModel extends Model
{
    use IsSitemappable;

    public function toSitemappableArray()
    {
        return [];
    }

    public function shouldBeSitemappable()
    {
        return true;
    }
}

toSitemappableArray

您需要返回一个包含(本地化)URL(s)的模型数组。

public function toSitemappableArray()
{
    return [
        'nl' => 'https://www.vursion.io/nl/testen/test-slug-in-het-nederlands',
        'en' => 'https://www.vursion.io/en/tests/test-slug-in-english',
    ];
}

这是一个使用ARCANDEDEV\Localization进行本地化路由和spatie\laravel-translatable将Eloquent模型本地化的模型示例。

public function toSitemappableArray()
{
    return collect(localization()->getSupportedLocalesKeys())->mapWithKeys(function ($key) {
        return [$key => localization()->getUrlFromRouteName($key, 'routes.your-route-name', ['slug' => $this->getTranslationWithoutFallback('slug', $key)])];
    });
}

shouldBeSitemappable(条件性可站点映射模型实例)

有时您可能需要在某些条件下才使模型可站点映射。例如,假设您有一个App\Models\Posts\Post模型。您可能只想允许“非草稿”和“已发布”帖子可站点映射。为此,您可以在您的模型上定义一个shouldBeSitemappable方法

public function shouldBeSitemappable()
{
    return (! $this->draft && $this->published);
}

从头开始重建站点地图

如果您将Laravel Sitemappable安装到现有项目中,您可能已经有一些数据库记录需要导入到您的站点地图中。Laravel Sitemappable提供了一个sitemappable:import Artisan命令,您可以使用它将所有现有的记录导入到您的站点地图中

php artisan sitemappable:import

添加非模型相关路由

您的项目很可能有与模型无关的路由。您可以通过扩展控制器并使用otherRoutes方法返回它们来添加这些URL。

要发布控制器到app/Http/Controllers/SitemappableController.php,请运行

php artisan vendor:publish --provider="Vursion\LaravelSitemappable\SitemappableServiceProvider" --tag=controllers

不要忘记在config/sitemappable.php配置文件中更改控制器位置

return [

    ...

    /*
     * If you're extending the controller, you'll need to specify the new location here.
     */
    'controller' => App\Http\Controllers\SitemappableController::class,

    ...

];

只需确保您返回一个类似以下示例的键值对数组数组

public function otherRoutes()
{
    return [
        [
            'nl' => 'https://www.vursion.io/nl/contacteer-ons',
            'en' => 'https://www.vursion.io/en/contact-us',
        ],
        ...
    ];
}

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

安全性

如果您发现任何安全相关的问题,请通过电子邮件support@vursion.io联系,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件