esign/laravel-redirects

一个Laravel包,用于控制从数据库或其他来源的重定向。

2.0.0 2024-08-05 10:09 UTC

This package is auto-updated.

Last update: 2024-09-05 10:23:16 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

此包提供了一种简单的方法来从数据库加载重定向,而不是在路由文件中定义它们。默认情况下,只有在原始请求结果为404时,包才会加载您的重定向。

安装

您可以通过composer安装此包

composer require esign/laravel-redirects

该包将自动注册服务提供者。

为了使重定向生效,您必须注册 Esign\Redirects\Http\Middleware\CheckForRedirects 中间件。

Laravel 11+

// bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
     $middleware->append(Esign\Redirects\Http\Middleware\CheckForRedirects::class);
})

旧版本(至Laravel 10)

// app/Http/Kernel.php

protected $middleware = [
    ...
    Esign\Redirects\Http\Middleware\CheckForRedirects::class,
];

此包附带一个迁移,用于存储您的重定向。如果您想修改此迁移,可以使用以下命令发布它

php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="migrations"

接下来,您可以发布配置文件

php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="config"

配置文件将作为 config/redirects.php 发布,内容如下

return [
    /**
     * This is the model used by the DatabaseRedirector.
     * It should implement the RedirectContract interface and extend the Model class.
     */
    'redirect_model' => Esign\Redirects\Models\Redirect::class,

    /**
     * This class provides the redicect url's to the CheckForRedirects middleware.
     * It should implement the RedirectorContract interface.
     */
    'redirector' => Esign\Redirects\Redirectors\DatabaseRedirector::class,

    'cache' => [
        /**
         * The key that will be used to cache the redirects.
         */
        'key' => 'esign.laravel-redirects.redirects',

        /**
         * The duration for which database redirects will be cached.
         */
        'ttl' => \DateInterval::createFromDateString('24 hours'),

        /**
         * The cache store to be used for database redirects.
         * Use null to utilize the default cache store from the cache.php config file.
         * To disable caching, you can use the 'array' store.
         */
        'store' => null,
    ],
];

用法

在数据库中定义重定向非常简单

Redirect::create([
    'old_url' => 'my-old-url',
    'new_url' => 'my-new-url',
]);

您也可以像在Laravel中那样定义路由参数

Redirect::create([
    'old_url' => 'my-old-url/{slug}',
    'new_url' => 'my-new-url/{slug}',
]);

当使用路由参数时,以下参数被Laravel保留,不能使用:destinationstatus

您甚至可以交换路由参数的顺序

Redirect::create([
    'old_url' => 'my-old-url/{slug}/{year}',
    'new_url' => 'my-new-url/{year}/{slug}',
]);

默认情况下将使用 302 状态码,但您也可以提供自定义的状态码

Redirect::create([
    'old_url' => 'my-old-url/{slug}/{year}',
    'new_url' => 'my-new-url/{year}/{slug}',
    'status_code' => 301,
]);

您还可以重定向到外部URL

Redirect::create([
    'old_url' => 'my-old-url',
    'new_url' => 'https://www.esign.eu',
]);

此包还允许您为您的路由定义 约束

Redirect::create([
    'old_url' => 'user/{id}',
    'new_url' => 'users/{id}',
    'constraints' => ['id' => '[0-9]+'],
]);

Redirect::create([
    'old_url' => 'nl/{any?}',
    'new_url' => 'nl-be/{any?}',
    'constraints' => ['any' => '.*'],
]);

此包还提供 DatabaseWildcardRedirector,允许您使用 * 作为通配符来定义重定向。这将自动应用一个约束来匹配任何尾随URL段

Redirect::create([
    'old_url' => 'my-old-url/*',
    'new_url' => 'my-new-url/*',
]);

缓存重定向

默认情况下,此包通过缓存数据库重定向24小时来确保高效性能。此缓存机制使用您在Laravel应用程序中配置的默认缓存驱动程序。

如果您想修改缓存持续时间或切换到不同的缓存存储,请参阅配置文件中的缓存设置。

清除重定向缓存

当您与 Esign\Redirects\Models\Redirect 模型交互时,将自动维护重定向缓存。但是,如果您在这些操作之外进行更改,则需要手动清除缓存

php artisan redirects:clear-cache

测试

composer test

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件