Laravel route redirect without closure for route cache

喜夏-厌秋 提交于 2020-02-28 06:14:24

问题


I have this code on my routes.php file that do a redirect. Though the problem is that whenever I ran php artisan route:cache command, it gives me an error of Unable to prepare route [article/{params}] for serialization. Uses Closure.

I know this has something to do with routes not allowing it to be cached if it have a closure. But how could I make a workaround for this redirect?

Route::get('article/{params}', function($params) {
    return Redirect::to($params, 301);
});

回答1:


Route caching does not work with Closure based routes. To use route caching, you must convert any Closure routes to use controller classes.

Route::get('article/{params}', 'HelperController@redirect');

in your controller you can have your redirect function like below:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelperController extends Controller
{
  public function redirect($params)
  {
    return Redirect::to($params, 301);
  }
}



回答2:


Since Laravel 5.5 you can use:

Route::redirect('/here', '/there', 301);

See the documentation under Redirect Routes.



来源:https://stackoverflow.com/questions/35613579/laravel-route-redirect-without-closure-for-route-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!