简体   繁体   中英

Rewriting route closure 301 redirects into controllers in Laravel

I have implemented 301 redirect in Laravel 5.5+ web.php for some pdf files, eg

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)

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

Laravel 5.5 Docs - Routing - Redirect Routes

Use Like this ( read my comment for this question )

Route::get('/old-url', function () {
    return Redirect::to('new-url');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM