vkovic/laravel-db-redirector

在 Laravel 中持久化和管理 HTTP 重定向

v1.0.2 2019-08-22 11:52 UTC

This package is auto-updated.

Last update: 2024-09-22 23:10:20 UTC


README

Build Downloads Stable License

使用数据库管理 Laravel 中的 HTTP 重定向

使用数据库规则来管理重定向。规则的设计与 Laravel 默认路由非常相似,因此语法非常容易理解。

兼容性

该软件包与 Laravel 版本 5.55.65.75.8 以及 PHP 版本 7.17.2 兼容。

安装

使用 composer 安装此软件包

composer require vkovic/laravel-db-redirector

需要将数据库重定向器中间件添加到中间件数组中

// File: app/Http/Kernel.php

// ...

protected $middleware = [
    // ...
    \Vkovic\LaravelDbRedirector\Http\Middleware\DbRedirectorMiddleware::class
];

运行迁移以创建存储重定向规则的表

php artisan migrate

用法:简单示例

创建重定向非常简单。您只需通过提供的 RedirectRule 模型添加数据库记录即可。重定向的默认状态码将是 301(永久移动)。

use Vkovic\LaravelDbRedirector\Models\RedirectRule;

// ...

RedirectRule::create([
    'origin' => '/one/two',
    'destination' => '/three'
]);

您还可以指定另一个重定向状态码

RedirectRule::create([
    'origin' => '/one/two',
    'destination' => '/three',
    'status_code' => 307 // Temporary Redirect
]);

您可以使用与原生 Laravel 路由相同的路由参数,它们将被传递到路上 - 从源到目的地

RedirectRule::create([
    'origin' => '/one/{param}',
    'destination' => '/two/{param}'
]);

// If we visit: "/one/foo" we will end up at "two/foo"

也可以使用可选参数

RedirectRule::create([
    'origin' => '/one/{param1?}/{param2?}',
    'destination' => '/four/{param1}/{param2}'
]);

// If we visit: "/one" we'll end up at "/four
// If we visit: "/one/two" we'll end up at "/four/two"
// If we visit: "/one/two/three" we'll end up at "/four/two/three"

链式重定向也可以工作

RedirectRule::create([
    'origin' => '/one',
    'destination' => '/two'
]);

RedirectRule::create([
    'origin' => '/two',
    'destination' => '/three'
]);

RedirectRule::create([
    'origin' => '/three',
    'destination' => '/four'
]);

// If we visit: "/one" we'll end up at "/four"

我们还可以一次性删除整个链(本例中的 3 个先前重定向记录)

RedirectRule::deleteChainedRecursively('/four');

有时可能会出现多个具有相同目的地的重定向。因此,建议用 try catch 块包围代码,因为在这种情况下将引发异常

RedirectRule::create(['origin' => '/one/two', 'destination' => '/three/four']);
RedirectRule::create(['origin' => '/three/four', 'destination' => '/five/six']);

// One more with same destination ("/five/six") as the previous one.
RedirectRule::create(['origin' => '/ten/eleven', 'destination' => '/five/six']);

try {
    RedirectRule::deleteChainedRecursively('five/six');
} catch (\Exception $e) {
    // ... handle exception
}

用法:高级

当给定的 URL 对应多个规则时,规则执行的顺序是什么。让我们通过这个简单的例子来看看

RedirectRule::create(['origin' => '/one/{param}/three', 'destination' => '/four']);
RedirectRule::create(['origin' => '/{param}/two/three', 'destination' => '/five']);

// If we visit: "/one/two/three" it corresponds to both of rules above,
// so, where should we end up: "/four" or "/five" ?
// ...
// It does not have anything to do with rule order in our rules table!

要解决这个问题,我们需要就简单的(且逻辑上合理的)规则优先级达成一致

优先级 1:没有命名参数的规则具有最高优先级

优先级 2:如果规则源有命名参数,那么命名参数更少的规则具有更高的优先级

优先级 3:如果规则源有相同数量的命名参数,那么命名参数在规则字符串末尾越近的规则具有优先级

所以让我们检查我们之前的案例,我们有

  • "/one/{param}/three" > "/four"
  • "/{param}/two/three" > "/five"

在这种情况下,两个规则都有相同数量的命名参数,但在第一个规则中 "{param}" 更靠近规则的末尾,因此它将具有优先级,我们将最终到达 "/four"。

贡献

如果您计划修改此 Laravel 软件包,您应该运行它附带的所有测试。最简单的方法是使用 Dockerdocker-composephpunit

首先,我们需要初始化 Docker 容器

docker-compose up -d

然后,我们可以运行测试并查看输出

docker-compose exec app vendor/bin/phpunit