autologic-web / redirect-bundle
用于处理重定向的 Symfony 扩展包
Requires
- php: ^5.6|^7.0
- symfony/config: ^2.7|^3.0|^4.0
- symfony/dependency-injection: ^2.7|^3.0|^4.0
- symfony/http-foundation: ^2.7|^3.0|^4.0
- symfony/http-kernel: ^2.7|^3.0|^4.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^5.0|^6.4
This package is auto-updated.
Last update: 2024-09-10 23:34:31 UTC
README
在迁移或应用/网站结构变更后配置重定向。
它捕获异常事件,如果异常类型为 NotFoundHttpException
,则将查找配置的规则,并向用户返回 RedirectResponse
响应以进行重定向。
适用于 Symfony ^2.7, ^3.0 或 ^4.0 以及 PHP ^5.6 或 ^7.0
该扩展包被设计得尽可能不干扰,因为进行此类操作的需求通常是临时的 - Google 建议将它们保留一年。只需包含扩展包并添加一个重定向规则配置块。
安装
通过 Composer 安装
$ composer require autologic-web/redirect-bundle
Symfony < 4
在 AppKernel.php
中包含扩展包
# app/AppKernel.php /** * Class AppKernel */ class AppKernel extends Kernel { /** * @return array */ public function registerBundles() { $bundles = [ // All your bundles new Autologic\Bundle\RedirectBundle\AutologicRedirectBundle(), ]; return $bundles; } }
Symfony 4
在 bundles.php
中包含扩展包
# config/bundles.php return [ // All your bundles Autologic\Bundle\RedirectBundle\AutologicRedirectBundle::class => ['all' => true], ];
配置
基本用法
# app/config.yml autologic_redirect: rules: - { pattern: '/old-route/', redirect: 'domain.com/new-route' }
pattern
(字符串,必需)
使用正则表达式匹配请求的完整 URI。服务捕获 404 异常,在无法匹配之前,使用 preg_match
在缺失页面的 URI 中查找匹配的规则。
redirect
(字符串,必需)
完整的重定向 URI。扩展包将根据传入的原始请求设置协议(http/https),以便轻松从开发环境切换到生产环境。
status
(整数,可选)
设置重定向的状态码(默认:301)。提示:在调试时使用 302 避免将 301 永久重定向缓存到浏览器中。
forwarding
(布尔值,可选)
将原始路由追加到重定向中(默认:false)。在以下情况下很有用:其他服务/应用程序已设置自己的重定向逻辑或不同域或路径上的路由结构相同。
absolute
(布尔值,可选)
强制绝对或相对重定向(默认:null/auto)。如果未设置,它将检测重定向中的主机名;如果重定向不包含主机名,则使用原始请求的主机名;如果包含主机名,则使用重定向原文。
protocol
(字符串,可选)
强制重定向协议(默认:null/auto)。如果未设置,它将检测原始请求的协议并使用该协议。
其他示例
# app/config.yml autologic_redirect: rules: # custom status code - { pattern: '/old-route/', redirect: 'domain.com/new-route', status: 302 } # forwarding: this will redirect to domain.com/new-route/old-route - { pattern: '/old-route/', redirect: 'domain.com/new-route', forwarding: true } # absolute: will force relative or absolute redirects # if false it will redirect to the route on the current host - { pattern: '/old-route/', redirect: '/new-route', absolute: false } # protocol: will force the protocol - { pattern: '/old-route/', redirect: '/new-route', protocol: 'ftp://' } # priority: this first rule will match first when a user visits /old-route/sub-route, the second acting as a fallback - { pattern: '/.*old-route\/sub-route', redirect: 'domain.com/new-route/sub-route' } - { pattern: '/.*old-route/', redirect: 'domain.com/new-route' } # match subdomains and more complex patterns and use parameters - { pattern: '/au\..+?\.[^\/]+.*blog\/old-australian-blog-post-on-any-domain-of-subdomain/', redirect: 'au.%base_domain%/news/new-australian-news-article' }
日志记录
要启用未匹配的 404 错误的日志记录,只需将记录器注入到 services.yml 中的监听器服务中。
# app/services.yml services: autologic_redirect.event.redirect_listener: class: Autologic\Bundle\RedirectBundle\Event\RedirectListener arguments: - '@autologic_redirect.service.redirect_service' - '@logger' tags: - { name: kernel.event_listener, event: kernel.exception }
这将以 notice
级别记录,以帮助检测没有设置任何重定向规则的 404。