Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Project structure

Before writing code it helps to know where things live. In a Laminas project every folder has one job, and new files almost always go into the module folder.

Folders in the project root

Folder or fileWhat it holds
configApplication configuration and the module list
config/autoloadShared configuration: global.php and local.php
moduleYour code, organised into modules
publicThe site root: index.php, styles, scripts, images
dataCache and other files the application creates
vendorPackages downloaded by Composer; never edited by hand
composer.jsonThe package list and class loading rules

The entry point

Every request to the site goes to public/index.php. It includes Composer's class loader, reads the configuration and runs the application:

// public/index.php (the main lines)
use Laminas\Mvc\Application;
 
chdir(dirname(__DIR__));
 
include __DIR__ . '/../vendor/autoload.php';
 
$appConfig = require __DIR__ . '/../config/application.config.php';
 
Application::init($appConfig)->run();

The chdir() line makes the project root the current folder, so paths in the configuration are written from the root: data/cache, config/autoload. You normally never change this file.

The config folder

FilePurpose
application.config.phpStartup settings: where modules and configuration come from, whether caching is on
modules.config.phpThe list of enabled modules
autoload/global.phpSettings shared by all machines
autoload/local.phpPasswords and keys of one particular machine

The Application module

A module is a folder with the code of one part of the site. The skeleton ships with one module:

module/Application/
├── config/
│   └── module.config.php
├── src/
│   ├── Controller/
│   │   └── IndexController.php
│   └── Module.php
└── view/
    ├── application/index/index.phtml
    ├── error/404.phtml
    ├── error/index.phtml
    └── layout/layout.phtml

config holds the module's configuration, src holds PHP classes and view holds page templates, called view scripts (files with the .phtml extension). layout/layout.phtml is the common frame of all pages, and error/404.phtml is the "not found" page.

The public folder

Only this folder is visible from the browser. The file public/css/style.css opens at /css/style.css. Everything else — code, configuration, passwords — sits one level above and cannot be reached by a link.

Common mistake

Do not put your own classes and view scripts into vendor and do not edit files inside it: the next composer install or composer update wipes the changes. Your code lives in module, settings live in config, and vendor belongs entirely to Composer.

Thanks for reading!

Donate to the site — we work hard for you.