<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Matthias Walter</title>
    <description>The latest articles on Forem by Matthias Walter (@mwltr).</description>
    <link>https://forem.com/mwltr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F70955%2F7bf4cdd2-dc52-43d2-ba19-52c25e9084a1.jpeg</url>
      <title>Forem: Matthias Walter</title>
      <link>https://forem.com/mwltr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mwltr"/>
    <language>en</language>
    <item>
      <title>Magento2 modules outside of magento-root</title>
      <dc:creator>Matthias Walter</dc:creator>
      <pubDate>Tue, 22 May 2018 18:22:08 +0000</pubDate>
      <link>https://forem.com/mwltr/magento2-modules-outside-of-magento-root-3hbb</link>
      <guid>https://forem.com/mwltr/magento2-modules-outside-of-magento-root-3hbb</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on:&lt;/em&gt; &lt;a href="https://www.mwltr.de/blog/2018/magento2-external-modules/" rel="noopener noreferrer"&gt;https://www.mwltr.de&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Quite recently I published a module which enables you to have your Magento2 modules outside of the magento-root.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-991587881478172672-326" src="https://platform.twitter.com/embed/Tweet.html?id=991587881478172672"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-991587881478172672-326');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=991587881478172672&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;If you missed it, here is the link to the module on github &lt;a href="https://github.com/mwr/ext-magento2-mwltr-filesystem-filedriver" rel="noopener noreferrer"&gt;mwr/ext-magento2-mwltr-filesystem-filedriver&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post is meant as a follow up, to show you how to achieve a setup with external local modules and how to use this module.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;Let's assume you want to achieve the following git repository directory-layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-project
    ├── magento
    |   └── composer.json
    └── src
        └── namespace
            └── my-module
                └── composer.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup would require your to run &lt;code&gt;composer install&lt;/code&gt; inside the magento sub-directory of your project-root &lt;code&gt;my-project&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This can be done from the project-root by executing &lt;code&gt;composer -d=magento install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The result will be magento copying its files inside the magento sub-directory.&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding modules from src
&lt;/h1&gt;

&lt;p&gt;To include modules from outside of the magento-root &lt;code&gt;my-project/magento&lt;/code&gt;, you have to do the following steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add local repository
&lt;/h2&gt;

&lt;p&gt;Let composer know where to search for your local modules by adding the local repository.&lt;/p&gt;

&lt;p&gt;You can do this by adding a json object to the repositories array with the type being &lt;code&gt;path&lt;/code&gt; and &lt;code&gt;url&lt;/code&gt; being the relative path to the modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"repositories"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"../src/*/*"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"composer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://repo.magento.com/"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find more information about the repository of type path in the &lt;a href="https://getcomposer.org/doc/05-repositories.md#path" rel="noopener noreferrer"&gt;composer documentation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add module require
&lt;/h2&gt;

&lt;p&gt;After you have added the repository, you need to add the composer package name of the local module you want to require.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"require"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"my-namespace/my-local-module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your run &lt;code&gt;composer -d=magento update&lt;/code&gt; right now, you might run into an error stating that no suitable version can be found. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Furyn73l01ti0r7j0fne2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Furyn73l01ti0r7j0fne2.png" alt="composer update error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case you might have to adjust the &lt;code&gt;minimum-stability&lt;/code&gt; setting to &lt;code&gt;dev&lt;/code&gt; When you do that, I also suggest setting the &lt;code&gt;prefer-stable&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"minimum-stability"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dev"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"prefer-stable"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now execute &lt;code&gt;composer -d=magento update&lt;/code&gt; and the module &lt;code&gt;my-namespace/my-local-module&lt;/code&gt; will be detected by composer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fryp6byq1cpjrdx58gymk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fryp6byq1cpjrdx58gymk.png" alt="composer update success"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Composer will create a symlink to your local module inside the &lt;code&gt;vendor/my-namespace&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;my-local-module -&amp;gt; ../../../src/my-namespace/my-local-module
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Issues with view or themes
&lt;/h2&gt;

&lt;p&gt;If left like that the module it self will work from a backend point of view. But if you add a view with layout updates and templates to your module, this will fail due the limitations of the getRelativePath method in &lt;code&gt;\Magento\Framework\Filesystem\Driver\File&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is where my module &lt;a href="https://github.com/mwr/ext-magento2-mwltr-filesystem-filedriver" rel="noopener noreferrer"&gt;&lt;code&gt;mwltr/ext-magento2-mwltr-filesystem-filedriver&lt;/code&gt;&lt;/a&gt; comes into play. &lt;br&gt;
This module fixes the &lt;code&gt;getRelativePath&lt;/code&gt;-implementation and generates a valid relative path.&lt;br&gt;
It is a bit tricky since the di.xml won't work for the &lt;code&gt;\Magento\Framework\Filesystem\Driver\File&lt;/code&gt; class. &lt;br&gt;
If you what to know more why that's the case have a look at the &lt;a href="https://github.com/mwr/ext-magento2-mwltr-filesystem-filedriver/blob/master/README.md" rel="noopener noreferrer"&gt;README.md&lt;/a&gt; of the module.&lt;/p&gt;

&lt;p&gt;The module is published on &lt;a href="https://packagist.org/packages/mwltr/ext-magento2-mwltr-filesystem-filedriver" rel="noopener noreferrer"&gt;packagist.org&lt;/a&gt; so to require it add a require statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"require"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"mwltr/ext-magento2-mwltr-filesystem-filedriver"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"my-namespace/my-local-module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run &lt;code&gt;composer -d=magento update&lt;/code&gt; again so the module is installed. &lt;/p&gt;

&lt;p&gt;You can also run &lt;br&gt;
&lt;code&gt;composer -d=magento require "mwltr/ext-magento2-mwltr-filesystem-filedriver"&lt;/code&gt;&lt;br&gt;
instead of manually updating the require section.&lt;/p&gt;

&lt;p&gt;From here on, you can either install magento the way you know it or in case are updating an existing magento installation, go right away and add a module with a view part.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Using this technique you can separate your project specific implementations from the magento-root or you can include external modules from any local path. &lt;br&gt;
Just be sure to think about the other developers working on that project, because they need the exact same paths, absolut paths or relative paths, on their machines as well.&lt;br&gt;
It might be a good idea to not reference directories outside of you repository root, to prevent a whole new set of problems ;-).&lt;/p&gt;

&lt;p&gt;The setup described in this post can be found on github: &lt;a href="https://github.com/mwr/demo-magento2-external-modules-simple" rel="noopener noreferrer"&gt;mwr/demo-magento2-external-modules-simple&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am using a similar, slightly optimized setup for my projects and it works quite well. &lt;/p&gt;

</description>
      <category>magento2</category>
      <category>php</category>
      <category>composer</category>
    </item>
  </channel>
</rss>
