Laravel路由重定向而不关闭路由缓存 [英] Laravel route redirect without closure for route cache
问题描述
我的routes.php
文件上有此代码,可以进行重定向.尽管问题在于,每当我运行php artisan route:cache
命令时,它都会给我一个Unable to prepare route [article/{params}] for serialization. Uses Closure.
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);
});
推荐答案
路由缓存不适用于基于Closure的路由.要使用路由缓存,必须将所有Closure路由转换为使用控制器类.
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);
}
}
这篇关于Laravel路由重定向而不关闭路由缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!