ruthgeridema/laravel-url-rewrites
Composer 安装命令:
composer require ruthgeridema/laravel-url-rewrites
包简介
Easy URL rewrites in your Laravel application
README 文档
README
Very easy to use URL rewrite package. Follow the instructions and you're good to go!
You can find an example project on my Github: view example project
This example project features the following:
- Eloquent observers to add URL rewrites automatically
- Usage of the trait
- Some use cases
Requirements
This package requires Laravel 5.8 or higher, PHP 7.2 or higher and a database that supports json fields and functions such as MySQL 5.7 or higher.
Installation
You can install the package via composer:
composer require ruthgeridema/laravel-url-rewrites
The package will automatically register itself.
Register the routes the feeds will be displayed on using the rewrites-macro.
You need to place it at the bottom of your routes file.
// In routes/web.php Route::rewrites();
You can publish the migration with:
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="migrations"
After the migration has been published you can create the url_rewrites table by running the migration:
php artisan migrate
You can optionally publish the config file with:
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="config"
This is the contents of the published config file:
<?php return [ 'table-name' => 'url_rewrites', 'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class, 'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class, 'cache' => true, 'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class, 'types' => [ 'product' => [ 'route' => 'product', 'attributes' => ['id'], ], 'category' => [ 'route' => 'category', 'attributes' => ['id'], ] ], ];
Laravel Nova
Using Laravel Nova? You can publish the Nova class to App/Nova with the following command
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="nova"
In the near future I will publish a Laravel Nova package with features like reindexing the URL rewrites.
Usage
Forward request
Let's say you've got a controller route 'product/{id}' and you have a product 'Apple Airpods' with id=5.
When you visit 'apple-airpods' this package will forward the request to the controller but keeps the clean url.
The following code adds this to the database:
UrlRewrite::create('apple-airpods', 'product/5')
Use named routes
You must specify the types in the config.
UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])
To regenerate the target path you can use
UrlRewrite::regenerateRoute($urlRewrite) UrlRewrite::regenerateAll() UrlRewrite::regenerateRoutesFromType($type)
To automatically add the URL attribute to an Eloquent model, you have to add the HasUrlRewrite trait to an Eloquent model.
You also need to add the urlRewriteType and optionally add 'url' to the appends array.
use HasUrlRewrite; public $urlRewriteType = 'category'; protected $appends = ['url'];
Once this is done you can simply call Model::find(1)->url to get the url of the model.
Redirect
301 redirect
UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)
302 redirect
UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)
Other functions
UrlRewrite::all() UrlRewrite::find($id) UrlRewrite::delete($id) UrlRewrite::update($data, $id) UrlRewrite::getByRequestPath('apple-airpods') UrlRewrite::getByTargetPath('product/5') UrlRewrite::getByTypeAndAttributes('product', ["id" => 5])
Testing
- Copy
.env.exampleto.envand fill in your database credentials. - Run
composer test.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email ruthger.idema@gmail.com instead of using the issue tracker.
Credits
Special thanks for Spatie for their guidelines and their packages as an inspiration
License
The MIT License (MIT). Please see License File for more information.
ruthgeridema/laravel-url-rewrites 适用场景与选型建议
ruthgeridema/laravel-url-rewrites 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.02k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2019 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「url」 「rewrite」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ruthgeridema/laravel-url-rewrites 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ruthgeridema/laravel-url-rewrites 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ruthgeridema/laravel-url-rewrites 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fine-tuning on the PHP built-in web server.
SQL optimizer and rewriter for laravel. - laravel 的 SQL 优化器和重写器。
Advanced TYPO3 redirect management for sys_redirect with regex and host/language-aware matching, CSV/.htaccess import/export, categories and priority, automatic slug redirect workflows, plus hit/log analytics.
WordPress Plugin rewrite URL
A Laravel helper to detect if the current route/path is active.
Provides a service and twig extension for getting short urls like http://your.host/~short
统计信息
- 总下载量: 7.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-07