Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

WallyJ's avatar

Changes to route in web.php not reflected on Shared Hosting

I changed a route on my web.php on a shared hosting account and refreshed the page URL and I don't see the change. I used a second browser to recreate the issue so it isn't browser cache.

What is the problem?

0 likes
33 replies
WallyJ's avatar

I can't run php artisan commands on my shared hosting. And I'm pretty sure cache:cache is not a proper command

WallyJ's avatar

There is not a routes.php file in my boostrap/cache directory

willvincent's avatar

If you are unable to run artisan commands, you may want to consider adding a route that allows you to do some basic cache clearing/etc.

Of course if the routes are already cached, adding a new route isn't going to be as easily accomplished.. in any case, good reason to not use shared hosting.

Could be your shared host is running some sort of opcode cache, so even if laravel isn't writing a cache file, file changes maybe aren't getting picked up by the php process..? Might be best to reach out to support with your host and ask them why changes to your codebase and not being reflected in what is being served.

WallyJ's avatar

I've gone down the route of using this code in my web.php file:

 //Clear route cache:
 Route::get('/route-clear', function() {
     $exitCode = Artisan::call('route:clear');
     return 'Routes cache cleared';
 });

 //Clear config cache:
 Route::get('/config-cache', function() {
     $exitCode = Artisan::call('config:cache');
     return 'Config cache cleared';
 }); 

// Clear application cache:
 Route::get('/clear-cache', function() {
     $exitCode = Artisan::call('cache:clear');
     return 'Application cache cleared';
 });

 // Clear view cache:
 Route::get('/view-clear', function() {
     $exitCode = Artisan::call('view:clear');
     return 'View cache cleared';
 });

However, when I go to /route-clear, I receive the following error: Unable to prepare route [api/user] for serialization. Uses Closure.

And when I google that I get a host of other forum articles about this issue being a possible bug in Laravel.

I tried to run: php artisan route:cache in my local dev environment to create a /bootstrap/cache/routes.php file to upload to my server to fix the issue, but received the same error message.

And I get the point about shared hosting. However, I'm a bit stuck with it for now.

Sti3bas's avatar

However, when I go to /route-clear, I receive the following error: Unable to prepare route [api/user] for serialization. Uses Closure.

Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes.

https://laravel.com/docs/6.x/controllers#route-caching

WallyJ's avatar

I understand that, as I have read on many forums, but the only way to run artisan commands via the routes file is to do so with the code I posted above, which includes Closures. So I seem stuck, but others seem to have used this to fix the issue.

Also, I don't want to necessarily cache routes. I just want my changed routes to work.

willvincent's avatar

Nah, you could just as well move the artisan calls into a controller.

Sinnbeck's avatar

Simple idea. Remove all those closures and just have these two. Cache all and remove all cache

Route::get('/artisan/optimize', 'ArtisanController@optimize');

Route::get('/artisan/clear', 'ArtisanController@clear');

And make a controller with 2 methods

public function optimize() 
{
    Artisan::call('optimize');
}

public function clear() 
{
    Artisan::call('optimize:clear');
}

Sadly I have no clever way of adding them if your installation does not recognize changes in web.php at all

WallyJ's avatar

The optimize command receives the same error message:

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

Sinnbeck's avatar

That means you still have some route that uses a closure. Hunt down that route :) remember to check api.php

WallyJ's avatar

I commented out everything that isn't a plain get or post. Still get the error. Plus, my bootstrap/cache folder doesn't even have a routes.php file so it doesn't look like they are cached. So confusing.

Sinnbeck's avatar

Open the file and look for a closure based route. The error said api/user

WallyJ's avatar

Where would I find that? Isn't that something built into the Auth routes? This was a complaint by someone on Github, that Laravel has Auth built in such a way that you can't even clear routes if you decide to use Auth.

WallyJ's avatar

Ahh... this is why that user called it a "bug".

Here is the function with the Closure:

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

It belongs to Laravel/Auth.

I didn't write that.

And again, I don't care about caching routes. I care about clearing any caches (though I don't see the file that normally holds them), so that my edits simply work.

WallyJ's avatar

Either way, it's not mine. It was written by Jeffrey Way himself. So it should NOT conflict with artisan commands.

jlrdw's avatar

You should be able to use putty (or other) to go to the command line.

Why would Jeffrey have that in there.

Maybe Taylor.

WallyJ's avatar

I meant Taylor. :)

I can Putty (SSH) into a shared server, but you can't run artisan commands there. I may have to switch to something like Digital Ocean, but this is just crazy that you can't change a route in web.php without having to run a command in the terminal to get it to work correctly. Seems silly.

Sinnbeck's avatar

Well be aware that it works perfectly for everyone else, without having to that. As I understood a previous post of yours, some place recommended it for shared hosting?

willvincent's avatar

just crazy that you can't change a route in web.php without having to run a command in the terminal to get it to work correctly. Seems silly.

I mean.. you shouldn't have to, unless routes were already cached.. it seems most likely that this is a hosting issue, not a laravel issue.

Have you reached out to your host yet to ask why changes aren't being reflected in your live code as I suggested earlier? Do you have the ability to restart the php process, or webserver process? If they've got some opcode cache in place it may be failing to allow new code to be evaluated.. Have seen that before, but I haven't used any shared hosts in over a decade :D

Sinnbeck's avatar

Well it seems to be a mix. Adding the new routes I suggested worked, but changing a certain route does not (as I understand)?

WallyJ's avatar

Thanks to everyone for their input. In an effort to push the launch forward, I assigned a subdomain to the site so I would no longer be dealing with the "~username" issue. This fixed, or at least negated the issue I was dealing with as the subdomain will work with routes that have a leading slash or not as it is the root of the site.

I will continue to learn and grow through the help of everyone in this community. Thanks again!!!

PS - I think I'll be moving to non-shared hosting on future projects.

1 like
Sinnbeck's avatar

Good to hear. Give me a shout-out if you have any issues getting digital ocean up and running :)

1 like
WallyJ's avatar

Thanks for the offer! I will certainly take you up on it. I'm so used to "shared hosting" where you purchase an account, and it already has Apache, mySQL, phpMyAdmin, SSL installed, etc, where, as I understand it you have to do a number of those things from scratch on Digital Ocean. Is that right?

Next

Please or to participate in this conversation.