Architecture
- A modular monolith
- Context-first DDD
- How a request flows
- Responsibilities
- Core rules
- Further reading
A modular monolith
Ambresoft Commerce is one Laravel application (a monolith) whose code is split into business contexts (modules). Each context owns its data and rules and talks to other contexts through small, explicit contracts. There are no microservices and no separate API backend: pages are rendered by Laravel controllers and shown with Inertia and React.
Optional features are add-ons: Composer-style packages loaded only while enabled. They use the same published contracts as core code. See Creating an add-on.
Context-first DDD
Code is organized by business context first, then by layer (decision A29 in the project’s decision register):
app/{Context}/
Domain/ plain PHP: entities, value objects, business rules, repository interfaces
Application/ plain PHP: use cases, typed input objects (Command/), ports (Contract/), services
Infrastructure/ Laravel and PostgreSQL: Eloquent records, mappers, adapters, service provider
Presentation/ HTTP controllers and requests, console commands
The contexts that exist today:
| Context | Owns |
|---|---|
Identity |
User accounts, registration, sign-in, password reset, email verification |
Authorization |
Staff roles, permissions and access-safety rules |
Localization |
Languages, the default language, interface translation overrides |
Catalog |
Categories, products, features, variations, brands, reviews, availability, imports |
Pricing |
Currencies, exchange rates, formatting, product sales |
Tax |
Taxes, rate areas and tax calculation |
Customer |
Customer profiles, addresses, groups, blacklist, call requests, wishlists |
Geography |
Shipping countries and regions |
Cart |
Carts, including abandoned carts offered to add-ons |
Order |
Orders, statuses, order emails, invoice and packing-slip layouts |
Payment |
Payment methods (cash on delivery, bank deposit, add-on providers), payment attempts and webhooks |
Shipping |
Shipping methods, carriers, shipments |
Marketing |
Banners, promotions, coupons, gift cards, stickers, newsletters, reels |
Website |
Pages, blogs, FAQs, menus, SEO redirect rules |
Storefront |
Themes, layouts, grids and blocks, the public catalog, comparison, SEO output, the storefront cache |
Search |
The search extension point, indexing, visibility, popular searches |
Reporting |
Reports |
Audit |
The central audit log |
Media |
Private image and file storage with metadata |
Notification |
Live admin notifications |
Settings |
General and company settings, AI settings |
Shared |
Cross-context contracts and the add-on (module) system |
Not every context has every layer: a folder exists only when it holds real code. Two folders are deliberately not contexts:
app/Admin: the administration is a presentation shell that spans contexts (controllers, requests, navigation, dashboard). It is not a business domain.app/Http: global middleware and the root controller.
What each layer may depend on
| Layer | May use | Must not use |
|---|---|---|
Domain |
its own Domain, App\Shared\Domain |
Laravel, Eloquent, HTTP, other contexts’ code |
Application |
its own Domain and Application, App\Shared\Domain, App\Shared\Application |
Laravel, Eloquent, HTTP |
Infrastructure |
Domain and Application contracts, Laravel, libraries | Presentation, App\Admin, App\Http |
Presentation |
Application operations, Laravel’s HTTP and console features | — |
This is enforced by a test. tests/Unit/ArchitectureTest.php reads every PHP file under app/ and fails when:
- a directory name is not PascalCase;
- a
DomainorApplicationclass references a namespace outside its allowed list; - a
DomainorApplicationclass calls a framework helper (app,config,auth,event,dispatch,request,response,route,session,collect,env,now,logger,dd,resolve); - an
Infrastructureclass imports presentation code.
Naming
- PHP directories are PascalCase and categories are singular:
Model,Repository,Command,Query,Service,Contract,Exception. HTTP code keeps Laravel’sControllersandRequests. - Laravel’s lowercase roots (
app,routes,database,config) stay as they are. - Create a folder or class only when real behavior needs it: no empty
Query/orService/folders, no placeholders for future features.
How a request flows
Route + middleware → Controller → (Form Request / input class) → Application contract (use case)
→ Domain rules → Infrastructure adapter (PostgreSQL) → events / jobs → Inertia → React page
A real example, creating a tax in the administration:
routes/admin.phpmapsPOST /admin/taxestoApp\Admin\Controllers\TaxController@store. Every admin route sits inside a group with the middlewareauth,auth.session,verifiedandcan:access-admin.-
The controller checks the operation’s own permission and calls the Application contract:
public function store(Request $request, TaxInput $input, TaxAdministration $taxes): RedirectResponse { Gate::authorize('taxes.create'); $id = $taxes->create((string) $request->user()->id, $input->data($request->all())); return redirect('/admin/taxes/'.$id.'/edit')->with('status', __('taxes.saved')); } App\Tax\Application\Contract\TaxAdministrationis a plain PHP interface.TaxInput(Infrastructure) validates the request and builds the typedSaveTaxDatacommand.App\Tax\Infrastructure\TaxServiceProviderbinds the interface to the PostgreSQL adapter:$this->app->bind(TaxAdministration::class, EloquentTaxAdministration::class);- The adapter checks the actor’s permission again (defense in depth), records the change in the audit log and writes
inside a PostgreSQL transaction. Business rules such as rate matching live in plain Domain services
(
App\Tax\Domain\Service\TaxCalculator,RateAreaMatcher). - The controller redirects; the edit page is rendered with
Inertia::render('Admin/Taxes/Form', [...]), which loadsresources/js/pages/Admin/Taxes/Form.tsx.
Simple reads do not need a service or repository: query classes such as TaxQueries return exactly the data a page
needs.
Responsibilities
| Piece | Responsibility |
|---|---|
| Controllers | Coordinate permission checks, input, the operation and the response. No pricing or stock algorithms |
| Form Requests / input classes | Validate and normalize transport input |
| Gates and policies | Server-side permissions, including every bulk target and add-on action |
| Services | Substantial operations, transaction boundaries, cross-model invariants |
| Queries | Complex reusable reads; ordinary Eloquent queries stay direct |
| Domain models | Plain PHP entities; Eloquent records and mappers live separately in Infrastructure |
| Events | Typed facts with explicit commit timing |
| Jobs | Retryable background work using stable identifiers and state checks |
| Persistence | Eloquent or the query builder over PostgreSQL with explicit constraints; no blanket repositories |
| Frontend | Separate admin and storefront page trees; share only suitable primitives and types |
Core rules
- The server owns business decisions. Prices, stock, taxes, totals, eligibility and validation are calculated in PHP and enforced in PostgreSQL. React displays results and collects input.
- Money is exact. Amounts are decimal strings handled by
App\Shared\Domain\Service\Decimal; neverfloat. - Integrity lives in PostgreSQL: foreign keys, unique and check constraints, and triggers where a rule spans rows (for example search change capture and storefront cache invalidation).
- Features, options, combinations and variations are different things. Keep their separate meanings (see the glossary).
- Optional features stay out of core. If a capability is optional for stores, it belongs in an add-on.
- No CQRS framework, command bus or event sourcing.
Command/andQuery/folders only organize code.
Further reading
The repository keeps the detailed engineering notes that this site summarizes: