3

I'm new to Laravel and now i manage project that was left by someone.

i try to add a function to API, what i edit :

1) Add a Method :

myproject/app/Http/Controllers/Api/ArticleController.php

2) Add Routes to controler :

myprojectmyproject/routes/api.php

However, when i try to run php artisan route:cache i got below error :

Route cache cleared! \n LogicException : Unable to prepare route [/] for serialization. Uses Closure.

my route file, 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');
    });

});

if i got error because of Uses Closure, why previous developer can populate route?

by run php artisan route:list i can see list of route that have ever made before.

any idea ?

===Update, Add routes/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');
});
questionasker
  • 2,536
  • 12
  • 55
  • 119
  • What's the content of the `routes/web.php` file? Maybe the closure route is there (the default one for laravel's fresh install). You get this error as closures cannot be serialized and when you do `php artisan route:cache` under the hood it selializes the routes files and combine them in a single one as he lookup would be quicker. – mdexp Jun 29 '19 at 12:35
  • @mdexp i have add code in question – questionasker Jun 29 '19 at 12:50
  • 1
    If you don't need the `/` route simply comment it out or remove it. Then the caching should work as expected. If you need it, move that route to a controller as you did with all the other routes – mdexp Jun 29 '19 at 12:51
  • *Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes.* From [route caching](https://laravel.com/docs/5.8/controllers#route-caching) – apokryfos Jun 29 '19 at 12:52
  • @mdexp what is it used for ? – questionasker Jun 29 '19 at 12:52
  • It is used to display the default Laravel page that is shown when you load the root of the website in a browser. You can look at it by yourself by opening up the browser and pointing it at your project's public folder – mdexp Jun 29 '19 at 12:54
  • check this solution ::: https://stackoverflow.com/questions/45266254/laravel-unable-to-prepare-route-for-serialization-uses-closure/64590185#64590185 – pankaj Oct 29 '20 at 11:27

2 Answers2

9

When you want to use route caching, you can't use closure to register routes in any file.

As you still have the default route from the fresh Laravel installation in your routes/web.php file, you get this error because when you do php artisan route:cache, Laravel under the hood selializes the routes files and combine them in a single one as the lookup would be quicker.

To solve the issue, you can simply remove the route if that is unneeded or move it to a controller like you did for all the others routes. The error should be gone then.

mdexp
  • 3,492
  • 2
  • 9
  • 20
2

Simply delete any route with a callback function like the default routes.

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

edit in

Route::middleware('auth:api')->get('/user', 'SomeController@someMethod');
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109