Home » Php » php – Unable to prepare route [api/user] for serialization. Uses Closure

php – Unable to prepare route [api/user] for serialization. Uses Closure

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m writing my project on Laravel. When I optimize the project, I have a problem :

Unable to prepare route [api/user] for serialization. Uses Closure.

I looked for any closures in web.php, but I didn’t find anything

    <?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/','[email protected]')->name('main');
Route::post('/','[email protected]')->name('verifyPost');

Route::get('/reviews', '[email protected]')->name('reviews');
Route::post('/reviews','[email protected]')->name('addReview');

Auth::routes();
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
    Route::get('/', '[email protected]')->name('admin');
    Route::get('/reviews', 'Admin\[email protected]')->name('admin.reviews');
    Route::get('/reviews/accepted/{id}','Admin\[email protected]')->where('id','\d+')->name('admin.accepted');
    Route::delete('/reviews/delete','Admin\[email protected]')->name('reviews.delete');
});
How to&Answers:

in api.php file search and comment this route you will not get error..

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

and also in web.php file route::group is also closure and also comment them for test

Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
    Route::get('/', '[email protected]')->name('admin');
    Route::get('/reviews', 'Admin\[email protected]')->name('admin.reviews');
    Route::get('/reviews/accepted/{id}','Admin\[email protected]')->where('id','\d+')->name('admin.accepted');
    Route::delete('/reviews/delete','Admin\[email protected]')->name('reviews.delete');
});

see what is closure

Answer:

Php routing cache command :

php artisan route:cache

if your application using controller based routes. It help for fast execution. But remember “Closure based routes cannot be cached”
So kindly convert your Closure routes to controller classes.

For more information

Answer:

Make sure to Check “routes/api.php”