laravellaravel-routinglaravel-cache

Laravel routes are not updating on server


When I upload my Laravel application to the server, routes are not being updated. For example, I have added the following simple route, at the very top of the other routes:

Route::get('/test', function () {
    return 'test';
});

But, when I open test, I get 404, instead of 'test'. Since the app is on the shared hosting, I am unable to run specific commands, however, I did try with

php artisan route:clear
php artisan config:clear
php artisan cache:clear

on local, and then to re-upload bootstrap and storage folders, however, this did not solve anything. Of course, everything works on local.

Thanks for any kind of help.


Solution

  • You can create a route in web.php for clearing all caches

    Pattern 01

    Route::get('/clear-cache', function() {
        $callMsg = '';
    
        Artisan::call('cache:clear');
        $callMsg .= "<h5 style='margin:10px 0 12px'>Cache cleared ...</h5>";
    
        Artisan::call('config:clear');
        $callMsg .= "<h5 style='margin:0 0 12px'>Config cleared ...</h5>";
    
        Artisan::call('config:cache');
        $callMsg .= "<h5 style='margin:0 0 12px'>Config Cache cleared ...</h5>";
    
        Artisan::call('route:cache');
        $callMsg .= "<h5 style='margin:0 0 12px'>Route Cache cleared ...</h5>";
    
        Artisan::call('route:clear');
        $callMsg .= "<h5 style='margin:0 0 12px'>Routes cleared ...</h5>";
    
        Artisan::call('view:cache');
        $callMsg .= "<h5 style='margin:0 0 12px'>View Cache cleared ...</h5>";
    
        Artisan::call('optimize');
        $callMsg .= "<h5 style='margin:0 0 12px'>Site optimized ...</h5>";
    
        return $callMsg;
    });
    

    Pattern 02

    Route::get('/clear-cache', function () {
        $cacheCommands = array(
            // 'event:clear',
            'view:clear',
            'cache:clear',
            'route:clear',
            'config:clear',
            'clear-compiled',
            'optimize:clear'
        );
    
        foreach ($cacheCommands as $command) {
            Artisan::call($command);
        }
    
       return "Cache cleared successfully";
    });
    

    When ever you want to clear the cache on the server side with Laravel Commands, just hit the Url /clear-cache (with your site url) like this

    e.g. http(s)://www.foo-boo.com/clear-cache

    It is working perfectly for me. Let me know if it works for you.

    Note: At first time, it will give you 404 error, but when you hit the same url second time, it will work perfectly.

    Hope it will work for you.