Symfony in vagrant environment

Submitted by oioioooi on 10/03/2022 - 20:31

Running Symfony based application in vagrant environment most likely would yield in cache issues.

In order to fix this, we need to instruct Symfony framework to place it's cache outside the vagrant synced folder. Luckily, it's easy and supported by the framework itself.

Check on How to Override Symfony's default Directory Structure (Symfony Docs)

With this in mind, the idea is to override both cache and log directories. A simple patch can be applied to the bootstrapping provisioner of the vagrant virtual machine to alter the framework behaviour.

diff --git a/src/Kernel.php b/src/Kernel.php
old mode 100644
new mode 100755
index 779cd1f..94b5032
--- a/src/Kernel.php
+++ b/src/Kernel.php
@@ -8,4 +8,14 @@ use Symfony\Component\HttpKernel\Kernel as BaseKernel;
 class Kernel extends BaseKernel
     use MicroKernelTrait;
+    public function getCacheDir(): string
+    {
+        return '/tmp/sf/cache';
+    }
+    public function getLogDir(): string
+    {
+        return '/tmp/sf/log';
+    }

Use the patch file below. This patch is applicable to Symfony framework version 6.

The patch should be placed in Symfony's sources root directory and applied either via patch or git. Make sure it's applied before composer install command.

$ patch -p1 < Kernel_vagrant.patch --force


$ git apply Kernel_vagrant.patch