lightframes/laravel-slugmaker
Composer 安装命令:
composer require lightframes/laravel-slugmaker
包简介
Generate slugs when saving Eloquent models in a separate table
关键字:
README 文档
README
This package provides a trait that will generate in a separate table a unique slug when saving any Eloquent model.
The slugs are generated with Laravels str_slug method, whereby spaces are converted to '-'.
Installation
You can install the package via composer:
composer require lightframes/laravel-slugmaker
! For Laravel < v5.5
Add the ServiceProvider to the providers array in config/app.php:
Lightframes\SlugMaker\SlugMakerServiceProvider::class,
Publish config file:
php artisan vendor:publish --tag=slugmaker-config
Publish the migration file:
php artisan vendor:publish --tag=slugmaker-migrations
and run the migration:
php artisan migrate
Usage
Configure
! Pay attention! Your model should not have a field named "slug"
Your Eloquent models should use the Lightframes\SlugMaker\ModelHasSlug trait.
The trait contains an method getSlugSourceFields() that you must implement yourself.
Also the trait contains public method slug() for relations your item-model with item-slug:
public function slug() { return $this->morphOne(\Lightframes\SlugMaker\Models\Slug::class, 'slugable'); }
Here's an example of how to implement the trait:
<?php namespace App\Models; use Lightframes\SlugMaker\ModelHasSlug; use Illuminate\Database\Eloquent\Model; class Article extends Model { use ModelHasSlug; protected $with = ['slug']; /** * Get the default fields for generating the slug. */ public function getSlugSourceFields() { return ['id', 'name', 'created_at']; // "124-article-test-slug-2017-12-26-135705" } }
Get & make slugs
You can to get slug with relation-method slug():
$article = Article::find(13); $slug = $article->slug->name;
Or use next scope-methods in your models:
getSlugName()findBySlug($slug)findOrFailBySlug($slug)getBySlugs(array $slugs)getArrayIdsBySlugs(array $slugs)makeSlug(string $str = '')
For example:
$slug = Article::find(13)->getSlugName(); $slug = Article::findBySlug('my-slug'); $slug = Article::getBySlugs(['my-slug-1', 'my-slug-2']); $slug = Article::getArrayIdsBySlugs(['my-slug-1', 'my-slug-2']);
Usage function helpers:
slug_get($slug)slug_get_models($slugs, $class = null)slug_get_id($slug, $class = null)slug_get_ids($slugs, $class = null)slug_get_grouped_class($attributes)slug_make($model, $slug = '')// if empty $slug - generate withgetSlugSourceFields(): arrayslug_delete($model)
Usage class SlugHelper:
$helper = new Lightframes\SlugMaker\SlugHelper(); $helper->getModel($slug, $modelClass = null); $helper->getModels(array $slugs, $modelClass = null); $helper->getId($slug, $modelClass = null); $helper->getIds(array $slugs, $modelClass = null); $helper->getIdsGroupedByClass(array $attributes); $helper->makeForModel($model, $slug = ''); $helper->deleteByModel($model);
The getIds() return the array:
[
1, 2, 8, 3
]
$attributes = [ App\Models\Article::class => ['slug-article-name',], App\Models\Page::class => ['slug-page-name-1', 'slug-page-name-2'], App\Models\Product::class => 'slug-product-name', ];
The getIdsGroupedByClass($attributes) return the array:
[
'App\Models\Article' => [1],
'App\Models\Page' => [2, 8],
'App\Models\Product' => [3],
]
Change log
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email stefano.lebbolo@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
lightframes/laravel-slugmaker 适用场景与选型建议
lightframes/laravel-slugmaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「slug」 「laravel-slugmaker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lightframes/laravel-slugmaker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lightframes/laravel-slugmaker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lightframes/laravel-slugmaker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
TitleWithSlugInput - Easy Permalink Slugs for the FilamentPHP Form Builder (PHP / Laravel / Livewire)
Adds slugify twig extensions to your craft install.
A library to extend Object capabilities.
A Laravel Filament Forms slug field.
Laravel 6 RuSlug, Transliteration RU -> ENG
String replacement class for Laravel 4
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-30