Anonymous
20 May, 2021

Why do any changes I make in laravel 7 route web.php file would not update on shared hosting?

1 Answer         3195 Views

Jiwan Thapa
21 May, 2021

Generally, I have experienced such issue with shared hosting where all the controllers and routes are loaded through cache. The view files seem to reflect the changes as soon as they are updated though.

The basic idea would be to create a closure function in your routes/web.php file and call all the artisan cache clearing methods from that function like:

Route::get('clear-cache', function() {
  Artisan::call('cache:clear');
  Artisan::call('optimize');
  Artisan::call('route:cache');
  Artisan::call('route:clear');
  Artisan::call('view:clear');
  Artisan::call('config:cache');
  Artisan::call('config:clear');
  return "All Cache Cleared !!!";
}

But, as you said, your route file is not reflecting the changes made, that won't work. Assuming your controllers are working fine, I would suggest you to call all the artisan methods from one of your controllers' working methods.

Going with the frontend index method would be a no brainer here. So, the working code would be:

Route::get('/',[FrontController::class,'index']);

If this is your route for frontend homepage, the index method in your controller would be:

public function index()
{
  Artisan::call('cache:clear');
  Artisan::call('optimize');
  Artisan::call('route:cache');
  Artisan::call('route:clear');
  Artisan::call('view:clear');
  Artisan::call('config:cache');
  Artisan::call('config:clear');
  return "All Cache Cleared !!!";
}

You will need to connect your controller to the laravel artisan method too. So, you should add the code given below on top of your coontroller.

use Illuminate\Support\Facades\Artisan;

Once you request the homepage route on your server, all the caches will be cleared. Then, you can comment out or remove those artisan methods and all your routes will be working as expected.

Moreover, sometimes, you may stumble upon page not found 404 errors, even when all the view files are there, make sure, you update the url section on your config/app.php file where the default url is defined as:

'url' => env('APP_URL', 'http://localhost'), 

Hope, this solution works out for everybody out there. Good luck !!!


0 Like         0 Dislike         0 Comment        


Leave a comment