Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Project structure

A Magento project consists of a dozen folders, but a developer writes code in only two of them. The rest hold the core, settings and files that Magento creates itself. It helps to learn right away what may be changed and what may not.

Project root

shop/
├── app/
│   ├── code/        your modules
│   ├── design/      your themes
│   └── etc/         installation settings
├── bin/magento      the console command
├── generated/       generated classes
├── pub/             the site root
├── var/             cache, logs, error reports
├── vendor/          the core and third-party packages
└── composer.json    the list of packages

What lives where

FolderWhat is in itMay you change it
app/codeYour modules: app/code/Vendor/ModuleYes, all the work happens here
app/designYour themesYes
app/etcThe env.php and config.php filesWith care
vendor/magentoCore modulesNo
generatedClasses created by MagentoNo, only delete
pub/staticBuilt styles, scripts and theme imagesNo, only delete
pub/mediaUploaded product imagesNo
varCache, logs in var/logNo, only read and delete

The app/code folder may be missing right after installation — create it by hand.

Files in app/etc

The app/etc/env.php file stores server settings: database access, the admin address, the encryption key. It is different on every server and is not added to version control.

The app/etc/config.php file holds the list of modules and whether each one is enabled:

// app/etc/config.php
return [
    'modules' => [
        'Magento_Store' => 1,
        'Magento_Catalog' => 1,
        'Acme_Hello' => 1,
    ],
];

A one means the module is enabled. The file is changed by bin/magento commands, not by a person, and is stored together with the project code.

Core modules

The core consists of the same kind of modules as the one you are about to write. The product catalog lives in vendor/magento/module-catalog, orders in vendor/magento/module-sales. In code the catalog module is called Magento_Catalog, and its classes start with Magento\Catalog.

These folders are worth reading: when it is unclear how an XML file or a class is built, a ready example can almost always be found in the core.

A module folder

Every module has the same structure. You will get to know it step by step; for now a general picture is enough:

FolderWhat is in it
etcXML configuration
ControllerActions that answer requests
ModelWorking with data
viewLayouts, templates, styles and scripts

Common mistake

The server was pointed at the project root instead of the pub folder. Then app/etc/env.php with the database password and the logs in var/log can be opened from a browser. The site root must always be the pub folder.

Thanks for reading!

Donate to the site — we work hard for you.