echoamirali / slugger
生成标准别名
dev-main
2022-08-02 15:48 UTC
Requires (Dev)
- laravel/legacy-factories: ^1.3
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-05 21:01:21 UTC
README
安装
步骤 1
- 方法 1:您可以通过 composer 安装此包
composer require echoamirali/slugger
- 方法 2:将此行添加到您的项目中的 Composer.json 文件
"echoamirali/slugger": "*"
然后运行以下命令使用 composer 下载扩展
$ composer update
步骤 2
打开 config/app.php 文件,并将此行添加到 providers 数组的末尾
Echoamirali\Slugger\SluggerServiceProvider::class,
步骤 3
然后在 config/app.php 中,并将此行添加到 aliases 数组的末尾
'Slugger' => Echoamirali\Slugger\SluggerFacade::class,
步骤 4
在您的项目目录中运行此命令
php artisan vendor:publish
在显示的消息中,找到 Slugger 的编号,输入相关编号然后按 Enter。
步骤 5
您可以为包进行配置,为此请转到 config/slugger.php
return [ /** * if do initail option is true, package do some operations on your string like * trims spaces, changes characters to lowercase , changes spaces to hyphen, ... **/ 'do_initial' => false, /** * if do translate option is true, package translate your string from 'translate_from' * option to 'translate_to' option at the first even before do initial **/ 'do_translate' => false, 'translate_from' => 'fa', 'translate_to' => 'en', /** * if do pattern option is true, package implement your string into * your pattern define in pattern option instead of #string# **/ 'do_pattern' => false, 'pattern' => '', /** * first of all you should know remain options only use in laravel models for * avoid repetition and when you put true in 'is_unique' option they work **/ 'is_unique' => false, // put your database column in this option, that you want be unique 'field' => 'slug', /** * this package use some symbols for avoid repetition and you can * choose which you want and put it in 'iteration_symbol' option. * available options for iteration_symbol are : * decimal, decimal_leading_zero, roman, numbers_in_word, ordinal_number **/ 'iteration_symbol' => 'decimal', //you can add your custom symbols in this option like alphabet 'custom_iteration_symbols' => [ 'alphabet' => range('a', 'z') ] ];
用法
您可以在任何地方使用此包。
首先使用类
use Slugger;
然后使用此模式对字符串进行别名化
$slug = Slugger::make($string);
Slugger 'make' 方法有两个可选参数,您可以在不使用配置文件或合并某些选项到配置文件选项时使用
/** this parameter has three available options * config_file : this is default option and when you choose this option, package use config file options * config_options : when you choose this option, you can put your options in config_options parameter * config_mixed : when you choose this option, you can put your options in config_options parameter and package merge your options with config file options **/ $config_status = 'config_options'; // when you choose option except config_file, you should put your options in this parameter $config_options = ['do_initial' => false, 'do_translate' => false]; $slug = Slugger::make($string, $config_status, $config_options);
您还可以在您的模型中使用 Slugger。
首先使用特质
use Echoamirali\Slugger\Traits\Slugger;
然后在您的模型中使用 Slugger 特质
class Post extends Model { use Slugger; }
如果您想为具有不同选项的某些模型使用 Slugger,您可以为任何模型定义特定的选项。
class Post extends Model { use Slugger; public $slugger_config = []; }
将您的模型配置放在 slugger 配置属性中,类似于 slugger 配置文件结构。例如,如果您在 slugger 配置属性包中只添加了 is_unique 选项,则使用 slugger 配置文件中剩余的选项。
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 echoamirali@gmail.com 而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。