What is the benefit of a pub directory in Magento 2

The question:

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.

Method 2

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

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment