sawastacks/ci4-slugify
Composer 安装命令:
composer require sawastacks/ci4-slugify
包简介
Simple Slugify Library for Codeigniter
README 文档
README
Contents
Simple Slugify Library for Codeigniter 4
This library provides a way of generating a unique slugs.
Background: What is a slug?
A slug is a simplified version of a string, typically URL-friendly. The act of "slugging" a string usually involves converting it to one case, and removing any non-URL-friendly characters (spaces, accented letters, ampersands, etc.). The resulting string can then be used as an identifier for a particular resource.
For example, if you have a blog with posts, you could refer to each post via the ID:
http://yourdomain.com/post/1
http://yourdomain.com/post/2
... but that's not particularly friendly (especially for SEO). You probably would prefer to use the post's title in the URL, but that becomes a problem if your post is titled "André & François took 55% of the whole space in REAC Company", because this is pretty ugly too:
The solution is to create a slug from the title and use that instead. You might want
to use Codeigniter's built-in \url_title('This is the title','-', true) helper function to convert that title/string into slug as follow:
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company
A URL like that will make users happier (it's readable, easier to type, etc.).
For more information, you might want to read this description on Wikipedia.
Slugs tend to be unique as well. So if you write another post with the same title, you'd want to distinguish between them somehow, typically with an incremental counter added to the end of the slug:
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company-1
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company-2
This keeps the URLs unique.
Installation
This package can be installed through composer require. Before install this, make sure that your are working with PHP >= 7.2 in your system.
Just run the following command in your cmd or terminal:
Install the package via Composer:
composer require sawastacks/ci4-slugify
Usage
After package installed in your Codeigniter project, you have now many ways you will use to generate a unique slugs for your blog posts or products. Just follow the following guides below that will teach the way you will use SlugService Class inside your controller.
Slugify Class
All the logic to generate slugs is handled
by the SawaStacks\CodeIgniter\Slugify class.
Generally, you don't need to access this class directly, although there is one static method that can be used to generate a slug for a given string without actually creating or saving an associated model. All you need to do, is to use the below strategies in your controller in order to generate a unique slugs.
<?php namespace App\Controllers; use App\Controllers\BaseController; use App\Models\Post; use SawaStacks\CodeIgniter\Slugify; class TestController extends BaseController { public function index(){ $post = new Post(); $title = 'André & François won mathematics competion'; $slug = Slugify::table('posts')->make($title); //When you use table name $slug = Slugify::model(Post::class)->make($title); //When you use model object //Result for $slug: andre-francois-won-mathematics-competion-3 $post->save([ 'title'=>$title, 'slug'=>$slug ]); } }
Below are possible examples you can use to generate unique slug in your controller using SlugService class:
<?php namespace App\Controllers; use App\Controllers\BaseController; use App\Models\Post; use SawaStacks\CodeIgniter\Slugify; class TestController extends BaseController { public function index(){ $post = new Post(); $title = 'André & François won mathematics competion'; /** Default way. Both lines will return same result */ $slug = Slugify::table('posts')->make($title); $slug = Slugify::model(Post::class)->make($title); /** When you specify slug field name. defaul is 'slug' */ $slug = Slugify::table('posts')->make($title,'post_slug'); $slug = Slugify::model(Post::class)->make($title,'post_slug'); /** When you specify divider/separator for generated slug. default is '-' */ $slug = Slugify::table('posts','id')->make($title); $slug = Slugify::model(Post::class,'id')->make($title); /** When you specify divider/separator for generated slug. default is '-' */ $slug = Slugify::table('posts')->separator('_')->make($title); $slug = Slugify::model(Post::class)->separator('_')->make($title); //Result for $slug: 1: andre-francois-won-mathematics-competion //Result for $slug: 2: andre_francois_won_mathematics_competion } }
Updating record
when you are not inserting new record, but you're updating any existing record, you'll need to add an extra function on chain which is sid(). Below is an example of how you can generate new slug of selected record.
<?php namespace App\Controllers; use App\Controllers\BaseController; use App\Models\Post; use SawaStacks\CodeIgniter\Slugify; class TestController extends BaseController { public function update_post(){ $id = $request->getVar('post_id'); $post = new Post(); $title = 'André & François won mathematics competion'; $slug = Slugify::model(Post::class)->sid($id)->make($title); } }
Class reference
SawaStacks\CodeIgniter\Slugify
table()
Arguments
- This method has two arguments. First argument will be the name of table which is required. Where the second argument is the primary key of the modal table. eg:
model('products','pid'). Default value of the second argument which is primary key is 'id'. This means that we suppose that the products table has id column/field in structure.
model()
Arguments
- This method has two arguments. First argument will be the instance of the model object which is required. Where the second argument is the primary key of the modal table. eg:
model(Product::class,'pid'). Default value of the second argument which is primary key is 'id'. This means that we suppose that the products table has id column/field in structure.
sid()
Arguments
- This method has one argument. This argument is required and must be an integer. eg:
sid(12). This number is the id of selected row.
separator()
Arguments
- This method has one argument. Here, you can specify the devider that will be included in generated slug. eg:
separator('_'). The default value is dash'-'.
make()
Arguments
- This method has two arguments. First argument is the string value from title which is required. Second argument is the slug field name. You can specify the name of field name in this method. The default value of second argument is
'slug'. eg:make('Title Of Your Choice','slug')
Copyright and License
This package was written by Sawa Stacks and is released under the MIT License.
Copyright (c) 2023 Sawa Stacks.
sawastacks/ci4-slugify 适用场景与选型建议
sawastacks/ci4-slugify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 306 次下载、GitHub Stars 达 7, 最近一次更新时间为 2024 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sluggable」 「codeigniter」 「slug」 「slugify」 「codeigniter4」 「ci4 slugify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sawastacks/ci4-slugify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sawastacks/ci4-slugify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sawastacks/ci4-slugify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
TitleWithSlugInput - Easy Permalink Slugs for the FilamentPHP Form Builder (PHP / Laravel / Livewire)
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
Adds slugify twig extensions to your craft install.
The CodeIgniter Redis package
A simple Cake3 plugin to slug fields and find records by slug.
A library to extend Object capabilities.
统计信息
- 总下载量: 306
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-26