Laravel HashSlug 项目教程
2024-08-27 11:54:53作者:庞眉杨Will
1、项目的目录结构及介绍
Laravel HashSlug 项目的目录结构如下:
laravel-hashslug/
├── config/
│ └── hashslug.php
├── src/
│ ├── HashSlug.php
│ └── HashSlugServiceProvider.php
├── tests/
│ └── HashSlugTest.php
├── .gitignore
├── LICENSE
├── README.md
├── composer.json
├── composer.lock
└── phpunit.xml
目录结构介绍
config/:包含项目的配置文件hashslug.php。src/:包含项目的主要源代码文件,包括HashSlug.php和HashSlugServiceProvider.php。tests/:包含项目的测试文件HashSlugTest.php。.gitignore:Git 忽略文件。LICENSE:项目的许可证文件。README.md:项目的说明文档。composer.json和composer.lock:Composer 依赖管理文件。phpunit.xml:PHPUnit 测试配置文件。
2、项目的启动文件介绍
项目的启动文件主要包括 HashSlugServiceProvider.php 和 HashSlug.php。
HashSlugServiceProvider.php
HashSlugServiceProvider.php 是 Laravel 服务提供者文件,负责注册和启动 HashSlug 服务。
namespace Afiqiqmal\LaraHashSlug;
use Illuminate\Support\ServiceProvider;
class HashSlugServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__.'/../config/hashslug.php' => config_path('hashslug.php'),
]);
}
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/hashslug.php', 'hashslug'
);
}
}
HashSlug.php
HashSlug.php 是 HashSlug 的核心逻辑文件,负责生成和解析 HashSlug。
namespace Afiqiqmal\LaraHashSlug;
use Hashids\Hashids;
class HashSlug
{
protected $hashids;
public function __construct()
{
$this->hashids = new Hashids(
config('hashslug.salt'),
config('hashslug.length'),
config('hashslug.alphabet')
);
}
public function encode($id)
{
return $this->hashids->encode($id);
}
public function decode($hash)
{
$decoded = $this->hashids->decode($hash);
return $decoded ? $decoded[0] : null;
}
}
3、项目的配置文件介绍
项目的配置文件位于 config/hashslug.php,包含了 HashSlug 的默认配置。
hashslug.php
return [
'salt' => env('HASHID_SALT', 'lara-hash-slug'),
'length' => env('HASHID_LENGTH', 12),
'alphabet' => env('HASHID_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'),
'unique' => env('HASHID_UNIQUE', true),
];
配置项介绍
salt:用于生成 HashSlug 的盐值,默认值为lara-hash-slug。length:生成的 HashSlug 的长度,默认值为 12。alphabet:用于生成 HashSlug 的字符集,默认值为abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890。unique:是否生成唯一的 HashSlug,默认值为true。
通过以上配置,可以灵活地调整 HashSlug 的生成规则。
登录后查看全文
热门项目推荐
暂无数据
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
540
3.77 K
Ascend Extension for PyTorch
Python
351
415
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
889
612
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
338
185
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
987
253
openGauss kernel ~ openGauss is an open source relational database management system
C++
169
233
暂无简介
Dart
778
193
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.35 K
758
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
115
141