问题
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