taylornetwork/model-slugger
Composer 安装命令:
composer require taylornetwork/model-slugger
包简介
Automatically add slugs to eloquent models
README 文档
README
Install
Using Composer
$ composer require taylornetwork/model-slugger
Usage
In a class that extends Illuminate\Database\Eloquent\Model add the TaylorNetwork\ModelSlugger\ModelSlugger trait.
This will require you to define the sluggerConfig() function which returns an array with minimum options being ['source' => 'model field to make slug from']
// app/ExampleModel.php namespace App; use Illuminate\Database\Eloquent\Model; use TaylorNetwork\ModelSlugger\ModelSlugger; class ExampleModel extends Model { use ModelSlugger; public function sluggerConfig() { return [ 'source' => 'name', ]; } }
Where name would be converted to a slug and placed into the column slug by default
Bind slug to route
You can bind the routes in your application to the slug rather than ID by adding
protected $sluggerRouteModelBind = true;
To your model, it will cause the routes to be looked up using the slug column.
Unique
To make all slugs unique add 'unique' => 'all' to the config array either in the model or in config/slugger.php
// app/ExampleModel.php public function sluggerConfig() { return [ 'source' => 'name', 'unique' => 'all', ]; }
Unique to a Parent
If you want slugs to only be unique based on a parent class, add 'unique' => 'parent', 'parent' => 'App\ParentClassName' to the model or config/slugger.php
For example you have a App\User model and a App\TodoList model where the a user can have many todo lists each with slugs. If we set the config to unique => all and every user makes a todo list named 'my todo list' the slugs will become increasingly long as they become unique.
To avoid this you can make the slugs unique if they are from the same parent.
// app/TodoList.php public function sluggerConfig() { return [ 'source' => 'name', 'unique' => 'parent', 'parent' => 'App\User', ]; }
Slugger Route Model Bind with Unique to a Parent
To accomplish route model binding you will need to add the code to find only slugs with the correct parent to your App\Providers\RouteServiceProvider class
For example
public function boot() { Route::bind('user', function ($value) { return App\User::findOrFail($value); // Code to find by ID or slug }); Route::bind('todoList', function ($slug, $route) { // $route->parameter('user') will return an instance of App\User return $route->parameter('user')->todoLists()->where('slug', $slug)->firstOrFail(); }); parent::boot(); }
Credits
- Main Author: Sam Taylor
License
The MIT License (MIT). Please see License File for more information.
taylornetwork/model-slugger 适用场景与选型建议
taylornetwork/model-slugger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 taylornetwork/model-slugger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 taylornetwork/model-slugger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-04-08