重写路由闭包 301 重定向到 Laravel 中的控制器 [英] Rewriting route closure 301 redirects into controllers in Laravel
本文介绍了重写路由闭包 301 重定向到 Laravel 中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在 Laravel 5.5+ web.php 中为一些 pdf 文件实现了 301 重定向,例如
I have implemented 301 redirect in Laravel 5.5+ web.php for some pdf files, e.g.
Route::get(
'old-url.pdf', function () {
return Redirect::to('new-url.pdf', 301);
});
它在开发环境中运行良好.
It works great on a dev environment.
但是,在生产站点上部署时,由于缓存(缓存不允许闭包的序列化),我收到诸如
However, when deployed on the production site, due to caching (caching doesn't allow serialization of closures), I get errors such as
LogicException: Noticed exception 'LogicException' with message 'Unable to prepare route [old-url.pdf] for serialization. Uses Closure.' in /XXX/vendor/laravel/framework/src/Illuminate/Routing/Route.php:880
我正在寻找有关如何使用控制器(或任何其他推荐方法)重写上述重定向的示例
I am looking for an example on how to re-write the redirect above using controllers (or any other recommended method)
推荐答案
我们在框架中内置了一个 RedirectController
来避免这个问题:
We have a RedirectController
built into the framework to allow you to avoid this issue:
Route::redirect('old-url.pdf', 'new-url.pdf', 301); // 301 by default
这篇关于重写路由闭包 301 重定向到 Laravel 中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文