schenke-io/laravel-url-cleaner
Composer 安装命令:
composer require schenke-io/laravel-url-cleaner
包简介
check and cleans url from seo or tracking data
关键字:
README 文档
README
Laravel URL cleaner - clean and concise
The Laravel URL Cleaner package sanitizes URLs by removing unnecessary SEO parameters, tracking information, and other clutter, ensuring clean and efficient URL handling in your Laravel applications.
To install just run:
composer require schenke-io/laravel-url-cleaner
Here a code example:
<?php use SchenkeIo\LaravelUrlCleaner\UrlCleaner; $shortUrl = (new UrlCleaner)->handle($longUrl);
Operation principle
The core UrlCleaner class iteratively applies a series of specialized
cleaner classes to a given URL. Each cleaner class performs a specific modification
to check and clean the URL for the following reasons:
- Reducing URL clutter: Removes unnecessary SEO parameters and tracking information.
- Improving data storage efficiency: Stores cleaner, more concise URLs.
- Enhancing performance: Optimizes URL processing and caching.
- Securing sensitive information: Prevents exposure of tracking parameters.
- Enhancing data analysis: Simplifies data analysis by removing noise from URLs.
This cleaner classes are highly extensible, allowing for customization and the creation of new modification types.
Config
A default configuration file can be installed and later modified, you can install it with:
php artisan url-cleaner:install
A typical result could be:
[
'cleaners' => [
MarketingBroad::class,
RemoveLongValues::class,
PreventInvalidHost::class
],
'max_length_value' => 40,
'masks' => ['dd3','vv67'],
'protected_keys' => ['search']
]
| key | type | description | cleaner |
|---|---|---|---|
| cleaners | array | list of cleaner classes applied to the given URL | any |
| max_length_value | int | values longer than this are removed by | RemoveLongValues |
| masks | array | additional masks to be used | RemoveConfigMasks |
| protected_keys | array | key names which are guard against removal | any |
| class name | # masks | description |
|---|---|---|
| Marketing00 | 68 | Marketing00 |
| Marketing01 | 106 | Marketing01 |
| Marketing02 | 43 | Marketing02 |
| Marketing03 | 170 | Marketing03 |
| Marketing04 | 111 | Marketing04 |
| MarketingBroad | 245 | MarketingBroad |
| MarketingNarrow | 333 | MarketingNarrow |
| MarketingUnique | 374 | MarketingUnique |
| PreventInvalidHost | - | PreventInvalidHost |
| PreventLocalhost | - | PreventLocalhost |
| PreventNonHttps | - | PreventNonHttps |
| PreventUserPassword | - | PreventUserPassword |
| RemoveConfigMasks | - | RemoveConfigMasks |
| RemoveLongValues | - | RemoveLongValues |
| RemoveSearch | - | RemoveSearch |
| ShortAmazonProductUrl | - | ShortAmazonProductUrl |
| SortParameters | - | SortParameters |
The use of masks
The core process of URL parameter removal utilizes specific masks.
| Description | Example mask |
|---|---|
| exact match of one query key on any domain | utm_campaign |
| match of some keys on any domain | utm_* *tm_* |
| exact match of one query key on one domain | utm_campaign@test.net |
| exact match of one query key on some domains | utm_campaign@test.* utm_campaign@*test.* |
| match of some keys on one domain | utm_*@test.net *x*@test.net |
| match of some keys on some domains | utm_*@test.* *x*@*test.* |
Some examples are outlined in the table below.
| Mask | URL 1 test.com/?a=1&b=2 |
URL 2 test.net/?a=1&abb=2 |
URL 3 test2.com/?a=1&b=2 |
|---|---|---|---|
| a | test.com/?b=2 | test.net/?abb=2 | test2.com/?b=2 |
| a* | test.com/?b=2 | test.net/ | test2.com/?b=2 |
| test.com@a | test.com/?b=2 | test.net/?a=1&abb=2 | test2.com/?a=1&b=2 |
| test.*@a | test.com/?b=2 | test.net/?abb=2 | test2.com/?a=1&b=2 |
Build your own cleaner by extending special classes
To extend the list of cleaners you can build your own
cleaners and put them in the config
file config/url-cleaner.php
The following cleaners are prepared to be extended for custom applications:
Prevent domain names
Extend PreventLocalhost and overwrite the $hostRegExes array with regular
expressions matching unwanted hostnames.
<?php use SchenkeIo\LaravelUrlCleaner\Cleaners\PreventLocalhost; class MyCleaner extends PreventLocalhost { protected array $hostRegExes = [ '/test\.com/', '/test\.net/', ]; }
Prevent schemes
Extend PreventNonHttps and overwrite the $allowedSchemes array with scheme
you allow to pass.
<?php use SchenkeIo\LaravelUrlCleaner\Cleaners\PreventNonHttps; class MyCleaner extends PreventNonHttps { protected array $allowedSchemes = [ 'https', 'http', 'sftp', ]; }
Use your own masks
Extend RemoveSearch and overwrite the $masks array with masks you want to exclude.
<?php use SchenkeIo\LaravelUrlCleaner\Cleaners\RemoveSearch; class MyCleaner extends RemoveSearch { protected array $masks = [ 'utm_*', 'test*', 'q@test.net' ]; }
Rewrite urls
Extend ShortAmazonProductUrl and overwrite the clean() method using
the class as an example.
<?php use SchenkeIo\LaravelUrlCleaner\Cleaners\ShortAmazonProductUrl; class MyCleaner extends ShortAmazonProductUrl { public function clean(UrlData &$urlData): void { // check if the hostname is right if (preg_match(/* regular expression */, $urlData->host)) { // check for the path to be replaced if (preg_match(/* regular expression */, $urlData->path, $matches)) { // your code $urlData->path = /* new path */; $urlData->fragment = ''; // clean if applicable $urlData->query = ''; // clean if applicable $urlData->parameter = []; // clean if applicable } } } }
AI Skills
| Title | Description |
|---|---|
| url-cleaner-development | Build and work with Laravel URL Cleaner features, including creating custom cleaners and managing URL sanitization logic. |
When to use
Use this skill when you need to:
- Extend the URL cleaning logic by creating new cleaner classes.
- Configure existing cleaners to handle specific URL parameters or tracking data.
- Integrate URL cleaning into Laravel applications using the provided Facade or Service Container.
- Troubleshoot or optimize the URL sanitization process.
Features
Creating Custom Cleaners
You can create new cleaners by extending the BaseCleaner class. Custom cleaners allow for domain-specific or parameter-specific logic that isn't covered by the default cleaners.
use SchenkeIo\LaravelUrlCleaner\Bases\BaseCleaner; use SchenkeIo\LaravelUrlCleaner\Data\UrlData; class MyCustomCleaner extends BaseCleaner { public function clean(UrlData &$urlData): void { // Custom logic to modify $urlData->query } }
Configuration-Driven Sanitization
The package is highly configurable. You can enable or disable cleaners, set maximum value lengths, and define protected keys that should never be removed.
Facade Support
The package provides a convenient UrlCleaner facade for easy integration and mocking in tests.
use SchenkeIo\LaravelUrlCleaner\Facades\UrlCleaner; $url = UrlCleaner::handle('https://example.com/?bad_param=1');
Markdown file generated by schenke-io/packaging-tools
schenke-io/laravel-url-cleaner 适用场景与选型建议
schenke-io/laravel-url-cleaner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「schenke-io」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 schenke-io/laravel-url-cleaner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 schenke-io/laravel-url-cleaner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 schenke-io/laravel-url-cleaner 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to simplify publishing github packages
Allow to plan, document and test model relations in Laravel
Use Google Sheet files as team-editor for Laravel translations and as data source for Laravel models using Laravel Sushi.
simple CURL based api client for applications without a SDK
Zlip Facade for Laravel
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-11
