11

Maybe I'm just not getting it but if my files are already cached then what would be the use of a pub directory in Magento 2? My only educated guess would be that the use of the fallback mechanism could be alleviated somewhat by having a known set of files for the theme.

Through reading the docs I don't a clear understanding of the benefit although I feel I have a strong grasp understanding dynamic, static and cached files.

As I understand it the cache is simply a defined known location of assets which led me to guess that the pub directory is a mechanism which standardizes the place of the files to reduce resources being used on the fallback mechanism.

1
  • great question, I wonder if the security thing is the only benefit or it also improves the speed Jul 6 '16 at 15:00
16

The pub directory is a security measure in order block public access to your root directory assets, namely the app directory. The reason that index.php is also located in the root directory is presumed to address individuals whose webhost make it difficult or impposible to change your webroot.

When utilizing the pub/index.php the app source folder will not be able to be accessed while it could be it be if index.php was being ran from the root directory. This practice is also seen in Symfony and Zend 2.

This way none of the Magento application configurations, templates, etc are exposed.

Generally, knowledge of the architecture in regards to the pub index.php vs root index.php are not needed for frontend developers on a team but understanding its basics are good to know.

1
10

Magento 2 comes with two index.php files where one file is inside root folder and another one is inside /pub folder. Magento 2 offers to run your application using any one of this file. If you choose to run your application using /pub/index.php then it is a good decision for security point of view as it restrict unauthorized direct access of application files.

As side note it's good to keep in mind that the /index.php file is used when running as developer mode and /pub/index.php is used when running as production mode

3
  • 1
    I appreciate pointing out that there are 2 index.php files but as to what was the benefit and use of the implementation using this type architecture, neither were addressed. Unless, I'm missing more into the subject I went ahead and answered the question from looking through Alan Storm, Zend and Symfony articles and docs. Feb 28 '16 at 19:45
  • regarding your last statement about dev/prod, can you confirm where this is done? I don't see anything like that Jul 6 '16 at 14:52
  • I'm with @brianellis why does it need to be this way? It doesn't seem to trick the browser anyway from what I can see.
    – ladle3000
    Feb 3 '18 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.