MMOframework
Why AI Playground Libraries Developers Agency
FR EN
Build a project live
Getting started · beginner

Configuration

config.json, dev.json, prod.json and local.json; keys named by class; path aliases. - **You will have:** a `Company` class of your own whose values come from the configuration, a different value in development, and a Contact page showing them, with its test. - **You need:** **Your first project** done: the `acme.portal` project. ## 1. Four files, in order `bootstrap.php` loads the configuration in this order, each file overriding the one before: | File | Read | Committed | |---|---|---| | `configs/config.json` | always | yes | | `configs/dev.json` | in a checkout (dev mode) | yes, never shipped in a release | | `configs/prod.json` | in a release (prod mode), from the server's `shared/` folder | the server's own | | `configs/local.json` | when it exists | never: git ignores it | So `config.json` holds what every machine shares, `dev.json` what only a checkout should do, and `local.json` this machine's secrets, such as a database password. A deployment never overwrites a server's `prod.json`. The files are JSON with `//` comments. ## 2. A key per class Every key of the configuration is the name of a class, and its values are that class's properties. You already have one: the project's `config.json` gives `MMO\client\Theme` its pages and its stylesheet. Do the same for a class of your own. Create `src/Company.php`, a module: a class the framework builds once and shares. ```php from=src/Company.php class Company extends aModule { /** @var string the company's name */ public string $name = 'Acme'; /** @var string where customers write to */ public string $email = ''; /** @var int the year it started */ public int $since = 2000; /** @return int how many years it has served its customers */ public function years(): int { return (int)date('Y') - $this->since; } } ``` Nothing loads a class by itself: `src/bootstrap.php` requires it, before anything that uses it. ```php from=src/bootstrap.php require(__DIR__ . '/Company.php'); ``` Then give it its values, in `configs/config.json`. A backslash is doubled in JSON: ```json from=configs/config.json "AcmePortal\\Company": { "name": "Acme Corporation", "email": "hello@acme.example", "since": 1998 }, ``` `Company::Shared()` is now built with these values. Never write `new Company()`: there would be two companies, and the second would not have its configuration. A key that is not a property of the class is an error, with the name it should have been: a typo does not go unnoticed. ## 3. Another value in development A checkout should never write to your real customers. Override the address in `configs/dev.json`; the other values stay `config.json`'s: ```json from=configs/dev.json "AcmePortal\\Company": { "email": "dev@acme.example" } ``` Objects are merged key by key. Lists are merged by position: write `"$[..]"` as the first item of a list to add to the one before instead (`["$[..]", "{AcmePortal}/more.scs"]`), and `"=key"` instead of `"key"` to replace a value whole. ## 4. Use it in a page A Contact page, `src/pages/contact/contact.phtml` (with its `index.json`, as in the previous guide): ```php from=src/pages/contact/contact.phtml use AcmePortal\Company; $company = Company::Shared(); ?> <h1>Contact <?= htmlspecialchars($company->name) ?></h1> <p>Write to <a href="mailto:<?= htmlspecialchars($company->email) ?>"><?= htmlspecialchars($company->email) ?></a>.</p> <p>We have served our customers for <?= $company->years() ?> years.</p> ``` <http://localhost:8000/contact> shows `dev@acme.example`: you are in a checkout. ## 5. Paths: aliases, not folders A path in the configuration starts with an alias rather than a folder: `{root}` is the project's folder, `{bin}` its `www/bin`, and each package has its own, registered by its `src/bootstrap.php`: `{AcmePortal}` is your `src/`. In a release, `src/` does not exist and `{AcmePortal}` is the same folder inside the project's `.phar`. That is why the project's configuration says: ```json from=configs/config.json "pages": ["{AcmePortal}/pages"], ``` In code, `\MMO\filename('{AcmePortal}/pages')` gives the real path, and `\MMO\url('contact')` the url of a page. ## 6. Test it `tests/unit/TestCompany.php`. The tests run in dev mode, so they see `dev.json`: ```php from=tests/unit/TestCompany.php public function testItsValuesAreTheConfigurations(): void { //configs/config.json $this->assertSame('Acme Corporation', Company::Shared()->name); $this->assertSame(1998, Company::Shared()->since); //configs/dev.json, read after it: the tests run in dev mode $this->assertSame('dev@acme.example', Company::Shared()->email); } ``` ```bash phpunit ``` ## Common mistakes - **`{root}/src/...` in a configuration.** Correct in a checkout, missing in production. Use the package's alias. - **A secret in `config.json` or `dev.json`.** Both are committed. It goes in `configs/local.json` on your machine, in the server's `prod.json` in production. - **A single backslash in a key.** `"AcmePortal\Company"` is not valid JSON: write `"AcmePortal\\Company"`. - **`new` on a module.** It is built without its configuration. Use `Shared()`. ## Going further - Next: **Pages**, then **Your first controller**, whose data comes from the configuration too. - [Starting a new application](https://mmoframework.fr/en/documentation/mmo.core/NewProject.md): the root bootstrap and the configuration files in detail. - [Array](https://mmoframework.fr/en/documentation/mmo.core/Array.md): how values are merged, `$[..]` and `=`.
Your next step

Try it yourself, or build the first one with us.

I'm a developer

Try it now. Nothing to install, nobody to call.

Build a project live

Read the guides

I lead a team

A working prototype of your AI product, on your data, in one week.

Prototype in a week

Open the page for your team