|
| 1 | +--- |
| 2 | +title: "ConfigProvider - Bootstrap Modern PHP Applications" |
| 3 | +description: "An overview of the ConfigProvider pattern used in Laminas/Mezzio-based applications, including Dotkernel, to bootstrap middleware pipelines and dependency injection." |
| 4 | +author: "Florin Bidirean" |
| 5 | +date_published: "2025-08-20" |
| 6 | +canonical_url: "https://www.dotkernel.com/architecture/configprovider-bootstrap-modern-php-applications/" |
| 7 | +category: "Architecture" |
| 8 | +language: "en" |
| 9 | +--- |
| 10 | + |
| 11 | +# ConfigProvider - Bootstrap Modern PHP Applications |
| 12 | + |
| 13 | +## TL;DR |
| 14 | + |
| 15 | +In PHP, a `ConfigProvider` is a class or callable that is part of an application's bootstrap process, returning configuration data that tells the platform which middleware should run, in what order, and under what conditions. Frameworks like Mezzio, Laminas, Slim, and the Dotkernel Headless Platform use ConfigProviders to declare middleware pipeline configuration, dependency injection mappings, and request handlers, which get merged together automatically during bootstrap (except in Dotkernel, where new ConfigProviders must be registered manually). |
| 16 | + |
| 17 | +## Where Is the ConfigProvider Used? |
| 18 | + |
| 19 | +Mezzio (formerly Zend Expressive), Laminas, Slim, the Dotkernel Headless Platform, and other middleware-based frameworks often have a `ConfigProvider` class. In Laminas/Mezzio specifically, each module or package may contain a `ConfigProvider` that returns: |
| 20 | + |
| 21 | +- Middleware pipeline configuration: |
| 22 | + - Middleware classes or service names. |
| 23 | + - Error-handling middleware, which should have the lowest priority. |
| 24 | + - Middleware groups or nested arrays. |
| 25 | +- Dependency injection mappings. |
| 26 | +- Request Handlers. |
| 27 | + |
| 28 | +Example structure used in Dotkernel: |
| 29 | + |
| 30 | +```php |
| 31 | +class ConfigProvider |
| 32 | +{ |
| 33 | + public function __invoke(): array |
| 34 | + { |
| 35 | + return [ /* ... */ ]; |
| 36 | + } |
| 37 | + |
| 38 | + public function getDependencies(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'factories' => [ /* ... */ ], |
| 42 | + 'invokables' => [ /* ... */ ], |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + public function getTemplates(): array |
| 47 | + { |
| 48 | + return [ |
| 49 | + 'paths' => [ /* ... */ ], |
| 50 | + 'error' => [ /* ... */ ], |
| 51 | + ]; |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +What each item means: |
| 57 | + |
| 58 | +| Item | Meaning | |
| 59 | +|---|---| |
| 60 | +| `dependencies` | Used by the dependency injector (e.g. laminas-servicemanager) to construct every requested service. | |
| 61 | +| `factories` | The factory builds the service. | |
| 62 | +| `invokables` | The service is built with `new` directly. | |
| 63 | +| `aliases` | Redirects to another service name. | |
| 64 | +| `delegators` | Wraps the original service. | |
| 65 | +| `templates` | Defines the paths for the template files. | |
| 66 | + |
| 67 | +## How the ConfigProvider Works |
| 68 | + |
| 69 | +The ConfigProvider is automatically picked up by the framework during application bootstrap: |
| 70 | + |
| 71 | +1. **Merge the global configuration** - All ConfigProviders are merged into one array. |
| 72 | +2. **Read the configuration array** - A call similar to `$config = $container->get('config') ?? [];` reads an array of entries. |
| 73 | +3. **Resolve item** - `$app->pipe()` is called to resolve one of the following: resolve the service name from the container, wrap the middleware if an array is provided, or call the closure or invokable object. |
| 74 | +4. **Handle errors** - The error-handling middleware is the last one in the pipeline, to make sure it can handle any exceptions. |
| 75 | +5. **Execute at runtime** - Laminas Stratigility iterates over the pipeline in the order it was registered. Each middleware can handle the request and return a response, or delegate execution to the next middleware in the pipeline, until a `ResponseInterface` is returned to the client. |
| 76 | + |
| 77 | +## Benefits |
| 78 | + |
| 79 | +- **Centralized setup** - Instead of hardcoding bootstrap code, it's declared in a config provider so it's easy to read, change, or extend. |
| 80 | +- **Modular** - Each package can ship with its own config without interfering with others. |
| 81 | +- **Container-friendly** - Works well with frameworks using DI containers like Laminas ServiceManager, PHP-DI, or Pimple. |
| 82 | +- **Standardized service definitions** - Consistent rules for object creation, separate from business logic. |
| 83 | +- **Auto-Discovery** - In Laminas/Mezzio, the ConfigAggregator automatically loads and merges all ConfigProviders. Dotkernel is an exception: new ConfigProviders have to be added manually in `config/config.php`, because all the initial ConfigProviders required to install the applications are already injected. |
| 84 | +- **Environment-agnostic** - Returns an array that defines dev, test, or prod environments. |
| 85 | +- **Testability** - The consistent, central configuration promotes isolated (e.g. per-module) testing, easier swapping of dependencies, and assertion of pipeline setup (e.g. checking if a config key is present). |
| 86 | + |
| 87 | +## FAQ |
| 88 | + |
| 89 | +**Q: What is a ConfigProvider in PHP?** |
| 90 | +A: It is a class that is part of an application's bootstrap process: a class or callable that returns configuration data telling the platform which middleware should run, in what order, and sometimes under what conditions. |
| 91 | + |
| 92 | +**Q: What does the ConfigProvider return in the Laminas/Mezzio ecosystem?** |
| 93 | +A: In the Laminas/Mezzio ecosystem, it's literally an array of configuration, settings, or anything else the application needs, and each module or package may contain its own ConfigProvider returning middleware pipeline configuration, dependency injection mappings, and request handlers. |
| 94 | + |
| 95 | +**Q: What is the difference between 'factories' and 'invokables' in the dependencies array?** |
| 96 | +A: `factories` will have the factory build the service, while `invokables` will use `new` directly. You can also use `aliases` to redirect to another service name and `delegators` to wrap the original service. |
| 97 | + |
| 98 | +**Q: How does the ConfigProvider get used during application bootstrap?** |
| 99 | +A: It is automatically picked up by the framework during bootstrap: all ConfigProviders are merged into one array, the configuration array is read, each item is resolved via `$app->pipe()`, the error-handling middleware is placed last in the pipeline, and at runtime Laminas Stratigility iterates over the pipeline in the order it was registered. |
| 100 | + |
| 101 | +**Q: Are new ConfigProviders auto-discovered in Dotkernel?** |
| 102 | +A: Dotkernel is an exception to the usual auto-discovery rule: new ConfigProviders have to be added manually in `config/config.php`, because all the initial ConfigProviders required to install the applications are already injected. |
| 103 | + |
| 104 | +**Q: What are the benefits of using a ConfigProvider?** |
| 105 | +A: Benefits include centralized setup instead of hardcoded bootstrap code, modularity so each package can ship its own config, container-friendliness with DI containers like Laminas ServiceManager, PHP-DI or Pimple, standardized service definitions, environment-agnostic configuration for dev/test/prod, and better testability of the pipeline setup. |
| 106 | + |
| 107 | +## Resources |
| 108 | + |
| 109 | +- [Mezzio Container](https://docs.mezzio.dev/mezzio/v3/features/container/config/) |
| 110 | +- [Laminas Config Aggregator](https://docs.laminas.dev/laminas-config-aggregator/config-providers/) |
| 111 | +- [PSR-15 (HTTP Server Request Handlers)](https://www.php-fig.org/psr/psr-15/) |
0 commit comments