vursion/laravel-sitemappable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vursion/laravel-sitemappable

Composer 安装命令:

composer require vursion/laravel-sitemappable

包简介

laravel-sitemappable

README 文档

README

Latest Version on Packagist Tests Total Downloads

Installation

You can install the package via composer:

composer require vursion/laravel-sitemappable

No need to register the service provider if you're using Laravel >= 5.5. The package will automatically register itself. Once the package is installed, you can register the service provider in config/app.php in the providers array:

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

You need to publish the migration with:

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

You should publish the config/sitemappable.php config file with:

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

This is the content of the published config file:

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,

];

Making a model sitemappable

The required steps to make a model sitemappable are:

  • Add the Vursion\LaravelSitemappable\IsSitemappable trait.
  • Define a public method toSitemappableArray that returns an array with the (localized) URL(s).
  • Optionally define the conditions when a model should be sitemappable in a public method shouldBeSitemappable.

Here's an example of a model:

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

You need to return an array with (localized) URL(s) of your model.

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',
    ];
}

This is an example of a model that uses ARCANDEDEV\Localization for localized routes in combination with spatie\laravel-translatable for making Eloquent models translatable.

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 (conditionally sitemappable model instances)

Sometimes you may need to only make a model sitemappable under certain conditions. For example, imagine you have a App\Models\Posts\Post model. You may only want to allow "non-draft" and "published" posts to be sitemappable. To accomplish this, you may define a shouldBeSitemappable method on your model:

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

Rebuild the sitemap from scratch

If you are installing Laravel Sitemappable into an existing project, you may already have database records you need to import into your sitemap. Laravel Sitemappable provides a sitemappable:import Artisan command that you may use to import all of your existing records into your sitemap:

php artisan sitemappable:import

Adding non-model associated routes

It's very likely your project will have routes that are not associated with a model. You can add these URLs by extending the controller and returning them via the otherRoutes method.

To publish the controller to app/Http/Controllers/SitemappableController.php run:

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

Don't forget to change the location of the controller in the config/sitemappable.php config file:

return [

    ...

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

    ...

];

Just make sure you return an array of arrays with key/value pairs like the example below:

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

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover any security related issues, please email jochen@celcius.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

vursion/laravel-sitemappable 适用场景与选型建议

vursion/laravel-sitemappable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Sitemap」 「laravel」 「vursion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vursion/laravel-sitemappable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vursion/laravel-sitemappable 我们能提供哪些服务?
定制开发 / 二次开发

基于 vursion/laravel-sitemappable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 46
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-17