Magento – Magento 2 does not allow linking Modules using symlinks

magento2module

I've added a local development module (separate git repo) to Magento 2 using symbolic links and it was working fine until I hit the PHTML templates. After heavy debugging I found out that the Magento 2 filesystem class (Magento\Framework\Filesystem\Directory\Read) uses an isExists() method combined with an getAbsolutePath() call to see whether the PHTML template exists. However, the getAbsolutePath() method checks whether the relative path exists within the Magento 2 filesystem and if not, it prepends the Magento 2 root to. it. In my case, the module lives in a separate repo /git/A while Magento lives in /site/B and this behaviour checks for the existence of a PHTML template in /site/B/git/A.

In short: PHTML templates that live outside of the Magento 2 root filesystem are not picked up, regardless of the System Configuration setting "Allow Symlinks" being turned on. It seems that setting is working, but only if the source of the symlink is still within the Magento 2 filesystem.

Has anybody else encountered this? What would be the best way to handle development in an external git repo?

Best Answer

Hackish workaround if you are using composer path repository for your module.

In registration.php put:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Module_Name',
    isset($file) ? dirname($file) : __DIR__
);

This will register your module under symlinked path in Magento's root vendor dir instead of its real path.

Related Topic