Namespace related problems in Shopware

Sometimes we may to manually clear cached Shopware files to fix namespace issues.

150 views
d

By. Jacob

Edited: 2024-03-18 14:43

Solution:

sudo rm -rf /var/www/shopware/var/cache/*

Problem:

A tremendously stupid situation that Shopware developers might get themselves into has to do with the confusion that rises as a result of Shopwares strange cache system. In fact, even in a development environment where you may often opt to disable caching, there still exist some cache in the /var/cache/, and this can be a source of confusion in more than one way. For one, basic namespace errors, even when you correct them, will still linger and present persistent errors like:

Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "Elasticsearch" from namespace "Shopware\Elasticsearch". Did you forget a "use" statement for another namespace? in ...

You might be smart and manually verify that composer has the right paths in its class map file, vendor/composer/autoload_classmap.php, only to find that everything is accurate. The problem must therefor be somewhere else. Shopware.

Clearing cache in Shopware

If you have worked with Shopware for some time, then you may guess at it beeing a caching issue. You try running the typical cache clear commands to no avail. E.g.

sudo -u www-data bin/console cache:clear
sudo -u www-data bin/console cache:pool:clear --all

Neither worked. Okay, let's clear redis cache too.

redis-cli FLUSHDB

Still nothing. Well, there is one thing that oddly works, and that's clearing the cached files manually. E.g:

sudo rm -rf /var/www/shopware/var/cache/*

I was personally told cache:pool:clear should take care of everything. Well, well, apparently not. And indeed, sometimes you can have a "cached" error in your own code that has since been corrected, and then you might not even be able to run the cache commands. Instead you should try the manual cache clear trick.

But wait. It gets more stupid; the naming of Shopware's file system paths drastically increases the risk of accidentally clearing the cache of your server operating system instead. Although this rarely seem to cause issues, as it will just be regenerated on reboot. Etc. You ought to be super careful when clearing cache this way. There is a big difference between /var/cache/* and /var/www/shopware/var/cache/*. Be very careful!

Lingering namespaces in the database

Another thing that might happen when working with custom developed plugins, is that plugin classes might get registered incorrectly in the database when the plugin is installed.

If you have an error, perhaps a typo in your plugin's namespace, the error will persist even after you correct the typo in your plugin's composer.json; this is because the plugin table in the database still has the old broken namespace path recorded!

To fix the problem you will either have to manually update the plugin table in the database, or you may get away with re-installing the plugin. Just be super careful, because with some plugins, the plugin-data will actually be deleted when you uninstall the plugin, and that might not be something you are interested in happening!

Tell us what you think:

  1. How to obtain the currently selected API language from Shopware vue components.
  2. How to access file system paths from storefront twig files in Shopware.
  3. How to get or change the currently selected sales channel in an Shopware sw-sales-channel-switch component.
  4. In this tutorial you will learn how to work with Shopware entities in a generic way from PHP, without having to specifically inject the repository in your services.xml file.

More in: Shopware