承接 esign/laravel-underscore-sluggable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

esign/laravel-underscore-sluggable

Composer 安装命令:

composer require esign/laravel-underscore-sluggable

包简介

Generate slugs when saving UnderscoreTranslatable models

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

This package adds support for spatie/laravel-sluggable package to models that use the UnderscoreTranslatable trait from the esign/laravel-underscore-translatable package.

Installation

You can install the package via composer:

composer require esign/laravel-underscore-sluggable

Upgrading

When updating to a new version, check the upgrade guide.

Usage

To support slug generation for models that use the UnderscoreTranslatable trait, you may add the HasTranslatableSlug trait to your models. Next up, you should define the getSlugOptions method on your model, which should be created using the createWithLocales method.

namespace App\Models;

use Esign\UnderscoreSluggable\HasTranslatableSlug;
use Esign\UnderscoreTranslatable\UnderscoreTranslatable;
use Illuminate\Database\Eloquent\Model;
use Spatie\Sluggable\SlugOptions;

class Post extends Model
{
    use UnderscoreTranslatable;
    use HasTranslatableSlug;

    public $translatable = [
        'title',
        'slug',
    ];

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::createWithLocales(['en', 'nl'])
            ->generateSlugsFrom('title')
            ->saveSlugsTo('slug');
    }
}

Generating a slug from a callback

You may also generate a slug from a callback by passing a closure to the generateSlugsFrom method. This callback will receive the model instance and the current locale as arguments:

namespace App\Models;

use Esign\UnderscoreSluggable\HasTranslatableSlug;
use Esign\UnderscoreTranslatable\UnderscoreTranslatable;
use Illuminate\Database\Eloquent\Model;
use Spatie\Sluggable\SlugOptions;

class Post extends Model
{
    use UnderscoreTranslatable;
    use HasTranslatableSlug;

    public $translatable = [
        'title',
        'slug',
    ];

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::createWithLocales(['en', 'nl'])
            ->generateSlugsFrom(function (Model $model, string $locale) {
                return $model->getTranslation('title', $locale) . '-' . $model->id;
            })
            ->saveSlugsTo('slug');
    }
}

For more configuration options, please refer to the spatie/laravel-sluggable documentation.

Self-healing URLs

Self-healing URLs combine the slug with the model's primary key (e.g. hello-world-5). When the slug changes, requests to the old URL are automatically redirected with a 308 to the canonical URL — so existing links never break.

Enable it by calling ->selfHealing() on your SlugOptions:

public function getSlugOptions(): SlugOptions
{
    return SlugOptions::createWithLocales(['en', 'nl'])
        ->generateSlugsFrom('title')
        ->saveSlugsTo('slug')
        ->selfHealing();
}

With this in place, route model binding will resolve hello-world-5 to the correct model. If the slug portion is stale (e.g. old-title-5), Laravel will issue a 308 Permanent Redirect to the current canonical URL.

Finding a model by slug

You may use findBySlug to retrieve a model by the slug of the active locale.

$post = Post::findBySlug('my-first-post');

To further scope the query, pass an additional query callback as the third argument:

$post = Post::findBySlug('my-first-post', ['*'], function ($query) {
    $query->where('status', 'published');
});

Testing

composer test

License

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

esign/laravel-underscore-sluggable 适用场景与选型建议

esign/laravel-underscore-sluggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.37k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-25