sevendays-digital/twill-redirects

dev-main 2022-10-28 15:09 UTC

This package is auto-updated.

Last update: 2024-08-28 19:18:14 UTC


README

此包为您的 Twill 安装添加重定向功能。

screenshot

安装

使用 composer 安装

composer require sevendays-digital/twill-redirects

(可选但如需重定向到其他模块则必须) 然后发布配置并设置模块

php artisan vendor:publish --tag=twill-redirects-config

最后运行迁移

php artisan migrate

使用方法

您现在可以通过 Twill 后端添加重定向。

使用方法取决于您的实现。您可以使用 RedirectManager 来确定

<?php
// Get a redirect for a specific request.
RedirectManager::getRedirectForRequest(request());

// Get a redirect for a specific path.
RedirectManager::getRedirectForPath('my/path');

或者如果您的前端由 Laravel 提供,您可以将中间件添加到 Http/Kernel.php

    protected $middlewareGroups = [
        'web' => [
            ...
            TwillRedirectMiddleware::class,
            ...
        ]
   ];

这将自动处理匹配的重定向。

但是有一个例外,那就是当您使用 browser 选择目标时。

对于这种情况,该包无法直接返回更多内容,但您可以手动拦截来处理重定向。

在您的 AppServiceProvider 中,您可以在 register 方法中添加以下内容

// Example:
RedirectManager::onTwillEntityRedirect(
    function(Model $model) {
        if ($model instance ExampleModel) {
          return route('my.route', ['id' => $model->id]);
        }
        return $model->slug ?? '';
    }
)