@wallyj maybe your routes are cached? php artisan route:cache
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?
I can't run php artisan commands on my shared hosting. And I'm pretty sure cache:cache is not a proper command
@wallyj do you have a bootstrap/cache/routes.php file?
There is not a routes.php file in my boostrap/cache directory
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.
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.
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.
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.
Nah, you could just as well move the artisan calls into a controller.
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
The optimize command receives the same error message:
Unable to prepare route [api/user] for serialization. Uses Closure.
That means you still have some route that uses a closure. Hunt down that route :) remember to check api.php
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.
Did you check api.php?
Check it how?
Open the file and look for a closure based route. The error said api/user
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 routes/api.php
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.
It belongs to Laravel/Auth.
No, it's not. It's included in a fresh app: https://github.com/laravel/laravel/blob/master/routes/api.php#L16
Either way, it's not mine. It was written by Jeffrey Way himself. So it should NOT conflict with artisan commands.
You should be able to use putty (or other) to go to the command line.
Why would Jeffrey have that in there.
Maybe Taylor.
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.
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?
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
Well it seems to be a mix. Adding the new routes I suggested worked, but changing a certain route does not (as I understand)?
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.
Good to hear. Give me a shout-out if you have any issues getting digital ocean up and running :)
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?
@wallyj or just use Laravel Forge which will do everything for you: https://forge.laravel.com/
Please or to participate in this conversation.