Laravel - 错误“无法为序列化准备路由 [/]。使用闭包。”
Posted
技术标签:
【中文标题】Laravel - 错误“无法为序列化准备路由 [/]。使用闭包。”【英文标题】:Laravel - Error "Unable to prepare route [/] for serialization. Uses Closure." 【发布时间】:2019-11-11 00:39:20 【问题描述】:我是 Laravel 的新手,现在我管理某人留下的项目。
我尝试向 API 添加一个函数,我编辑的内容:
1) 添加方法:
myproject/app/Http/Controllers/Api/ArticleController.php
2) 将 Routes 添加到控制器:
myprojectmyproject/routes/api.php
但是,当我尝试运行 php artisan route:cache
时,出现以下错误:
路由缓存已清除! \n LogicException:无法为序列化准备路由 [/]。使用闭包。
我的路由文件,myproject/routes/api.php:
Route::group (['prefix' => 'v1', 'middleware' => 'ValidateHeaderSignature'], function()
Route::group(['prefix' => 'auth'], function()
Route::post('/login', 'Api\AuthController@login');
Route::post('/register', 'Api\AuthController@register');
Route::post('/login-social-media', 'Api\AuthController@loginSocialMedia');
Route::post('/forgot-password', 'Api\AuthController@forgotPassword');
Route::group(['middleware' => 'jwt.auth'], function()
Route::patch('/change-password', 'Api\AuthController@changePassword');
Route::post('/logout', 'Api\AuthController@logout');
);
);
Route::group(['prefix' => 'foundation-donate'], function()
Route::get('/', 'Api\FoundationDonateController@index');
);
Route::group(['prefix' => 'greeting-chat'], function()
Route::get('/', 'Api\GreetingChatController@index');
);
Route::group(['prefix' => 'prayer-time'], function ()
Route::get('/', 'Api\PrayerTimeController@index');
Route::get('/montly', 'Api\PrayerTimeController@getMontlyPrayerTimes');
);
Route::group(['prefix' => 'asmaul-husna'], function ()
Route::get('/', 'Api\AsmaulHusnaController@index');
);
Route::group(['prefix' => 'guidance'], function ()
Route::get('/zikir', 'Api\GuidanceController@zikirGuidances');
Route::get('/prayer', 'Api\GuidanceController@prayerGuidances');
);
Route::group(['prefix' => 'duas'], function ()
Route::get('/', 'Api\DuasController@index');
Route::get('/index', 'Api\DuasController@index');
Route::get('/all', 'Api\DuasController@allPrayers');
Route::get('/category/category', 'Api\DuasController@category');
Route::get('/show/id', 'Api\DuasController@show');
);
Route::group(['prefix' => 'zakat'], function ()
Route::get('/', 'Api\ZakatController@index');
Route::get('/index', 'Api\ZakatController@index');
Route::get('/all', 'Api\ZakatController@allPrayers');
Route::get('/category/category', 'Api\ZakatController@category');
Route::get('/show/id', 'Api\ZakatController@show');
);
Route::group(['prefix' => 'playlist'], function ()
Route::get('/zikir', 'Api\PlaylistSongController@playlistZikir');
Route::get('/shalawat', 'Api\PlaylistSongController@playlistShalawat');
Route::get('/duas', 'Api\PlaylistSongController@playlistDuas');
Route::get('/murottal', 'Api\PlaylistSongController@playlistMurottal');
Route::get('/songs', 'Api\PlaylistSongController@playlistSongs');
);
Route::group(['prefix' => 'dzikir'], function()
Route::get('/primary', 'Api\DzikirController@primaryDzikir');
Route::get('/my-dzikir', 'Api\DzikirController@myDzikir');
Route::get('/categories', 'Api\DzikirController@dzikirCategories');
Route::group(['middleware' => 'jwt.auth'], function()
Route::get('/point-total', 'Api\DzikirController@pointTotal');
Route::get('/histories', 'Api\DzikirController@histories');
Route::get('/total-dzikir-history', 'Api\DzikirController@totalDzikirHistory');
Route::post('/post-dzikir', 'Api\DzikirController@postDzikir');
);
);
Route::group(['prefix' => 'sadaqah'], function()
Route::group(['middleware' => 'jwt.auth'], function()
Route::get('/histories', 'Api\DzikirController@sadaqahHistories');
);
);
Route::group(['prefix' => 'article'], function()
Route::get('/', 'Api\ArticleController@index');
Route::get('/daily-reflection', 'Api\ArticleController@getDailyReflection');
Route::get('/get-random', 'Api\ArticleController@getRandom');
);
Route::group(['prefix' => 'notification'], function()
Route::get('/quote', 'Api\NotificationController@prayerQuotes');
);
Route::group(['prefix' => 'user', 'middleware' => 'jwt.auth'], function()
Route::get('/show', 'Api\UserController@show');
Route::patch('/update-profile', 'Api\UserController@update');
);
Route::group(['prefix' => 'master'], function()
Route::get('/location', 'Api\MasterController@location');
);
);
如果我因为Uses Closure 而出错,为什么以前的开发人员可以填充路由?
通过运行php artisan route:list
我可以看到以前做过的路线列表。
有什么想法吗?
===更新,添加路由/web.php
Route::get('/', function ()
return view('welcome');
);
Auth::routes(['register' => false]);
Route::get('/home', 'HomeController@index')->name('home');
Route::get('register/activation/code', 'Auth\\RegisterController@activation')->name('register.activation');
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function()
Route::get('/user-apps/list-index', ['as' => 'user-apps.list-index', 'uses' => 'Admin\\UserAppsController@listIndex']);
Route::get('/user-apps/resend-confirmation', ['as' => 'user-apps.resend-confirmation', 'uses' => 'Admin\\UserAppsController@resendConfirmation']);
Route::resource('/user-apps', 'Admin\\UserAppsController');
Route::get('/user/list-index', ['as' => 'user.list-index', 'uses' => 'Admin\\UserController@listIndex']);
Route::resource('/user', 'Admin\\UserController');
Route::get('/dzikir-playlist-category/list-index', ['as' => 'dzikir-playlist-category.list-index', 'uses' => 'Admin\\DzikirPlaylistCategoryController@listIndex']);
Route::resource('/dzikir-playlist-category', 'Admin\\DzikirPlaylistCategoryController');
Route::get('/dzikir-playlist/list-index', ['as' => 'dzikir-playlist.list-index', 'uses' => 'Admin\\DzikirPlaylistController@listIndex']);
Route::resource('/dzikir-playlist', 'Admin\\DzikirPlaylistController');
Route::get('/dzikir-playlist-homepage/list-index', ['as' => 'dzikir-playlist-homepage.list-index', 'uses' => 'Admin\\DzikirPlaylistHomepageController@listIndex']);
Route::resource('/dzikir-playlist-homepage', 'Admin\\DzikirPlaylistHomepageController');
Route::get('/dzikir-playlist-my-zikir/list-index', ['as' => 'dzikir-playlist-my-zikir.list-index', 'uses' => 'Admin\\DzikirPlaylistMyZikirController@listIndex']);
Route::resource('/dzikir-playlist-my-zikir', 'Admin\\DzikirPlaylistMyZikirController');
Route::get('/greeting-chat/list-index', ['as' => 'greeting-chat.list-index', 'uses' => 'Admin\\GreetingChatController@listIndex']);
Route::resource('/greeting-chat', 'Admin\\GreetingChatController');
Route::get('/foundation-donate/list-index', ['as' => 'foundation-donate.list-index', 'uses' => 'Admin\\FoundationDonateController@listIndex']);
Route::resource('/foundation-donate', 'Admin\\FoundationDonateController');
Route::get('/asmaul-husna/list-index', ['as' => 'asmaul-husna.list-index', 'uses' => 'Admin\\AsmaulHusnaController@listIndex']);
Route::resource('/asmaul-husna', 'Admin\\AsmaulHusnaController');
Route::get('/guidance/list-index', ['as' => 'guidance.list-index', 'uses' => 'Admin\\GuidanceController@listIndex']);
Route::resource('/guidance', 'Admin\\GuidanceController');
Route::get('/content-category/list-index', ['as' => 'content-category.list-index', 'uses' => 'Admin\\ContentCategoryController@listIndex']);
Route::resource('/content-category', 'Admin\\ContentCategoryController');
Route::get('/duas/list-index', ['as' => 'duas.list-index', 'uses' => 'Admin\\DuasController@listIndex']);
Route::resource('/duas', 'Admin\\DuasController');
Route::get('/zakat/list-index', ['as' => 'zakat.list-index', 'uses' => 'Admin\\ZakatController@listIndex']);
Route::resource('/zakat', 'Admin\\ZakatController');
Route::get('/quote/list-index', ['as' => 'quote.list-index', 'uses' => 'Admin\\QuoteController@listIndex']);
Route::resource('/quote', 'Admin\\QuoteController');
Route::get('/playlist-song-category/list-index', ['as' => 'playlist-song-category.list-index', 'uses' => 'Admin\\PlaylistSongCategoryController@listIndex']);
Route::resource('/playlist-song-category', 'Admin\\PlaylistSongCategoryController');
Route::get('/playlist-song/list-index', ['as' => 'playlist-song.list-index', 'uses' => 'Admin\\PlaylistSongController@listIndex']);
Route::resource('/playlist-song', 'Admin\\PlaylistSongController');
Route::get('/album/list-index', ['as' => 'album.list-index', 'uses' => 'Admin\\AlbumController@listIndex']);
Route::resource('/album', 'Admin\\AlbumController');
Route::get('/artist/list-index', ['as' => 'artist.list-index', 'uses' => 'Admin\\ArtistController@listIndex']);
Route::resource('/artist', 'Admin\\ArtistController');
Route::get('/article/list-index', ['as' => 'article.list-index', 'uses' => 'Admin\\ArticleController@listIndex']);
Route::resource('/article', 'Admin\\ArticleController');
);
【问题讨论】:
routes/web.php
文件的内容是什么?也许关闭路线就在那里(laravel 全新安装的默认路线)。您会收到此错误,因为无法序列化闭包,并且当您在后台执行 php artisan route:cache
时,它会序列化路由文件并将它们组合成一个文件,因为他的查找速度会更快。
@mdexp 我添加了有问题的代码
如果您不需要 /
路由,只需将其注释掉或删除即可。然后缓存应该按预期工作。如果您需要它,请像处理所有其他路由一样将该路由移至控制器
基于闭包的路由不能被缓存。要使用路由缓存,您必须将任何 Closure 路由转换为控制器类。来自route caching
@mdexp 它是干什么用的?
【参考方案1】:
当你想使用路由缓存时,你不能使用闭包在任何文件中注册路由。
由于您的 routes/web.php
文件中仍然有来自全新 Laravel 安装的默认路由,因此您会收到此错误,因为当您 do php artisan route:cache
时,Laravel 在引擎盖下序列化路由文件并将它们组合成一个文件,因为他查找会更快。
要解决此问题,您可以简单地删除路由(如果不需要)或将其移至控制器,就像对所有其他路由所做的那样。 那么错误应该消失了。
【讨论】:
【参考方案2】:使用回调函数删除任何路由,就像默认路由一样。
Route::middleware('auth:api')->get('/user', function (Request $request)
return $request->user();
);
编辑
Route::middleware('auth:api')->get('/user', 'SomeController@someMethod');
【讨论】:
以上是关于Laravel - 错误“无法为序列化准备路由 [/]。使用闭包。”的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 7:无法为序列化准备路由 [api/user]。使用闭包。 (逻辑异常)[重复]
Laravel jordanmiguel/laravel-流行错误[重复]
php [Laravel - 自定义错误消息] Laravel 5.4其他登录条件 - 添加自定义错误消息。 #All #Script #Laravel #Php