movor/laravel-db-redirector

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

v0.3.1 2018-12-01 12:32 UTC

This package is not auto-updated.

Last update: 2024-09-15 05:28:47 UTC


README

Build Downloads Stable License

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

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

❗❗❗ 正在迁移GIT仓库 ❗❗❗

此包的 repo 和维护将继续在

vkovic/laravel-db-redirector.

🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚 🚚

兼容性

该包与Laravel版本 >= 5.1 兼容

安装

通过composer安装包

composer require movor/laravel-db-redirector

需要在服务提供者中注册包

// File: config/app.php

// ...

/*
 * Package Service Providers...
 */

// ...

Movor\LaravelDbRedirector\Providers\DbRedirectorServiceProvider::class,

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

// File: app/Http/Kernel.php

// ...

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

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

php artisan migrate

使用:简单示例

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

use Movor\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"。