PHPackages                             ctw/ctw-middleware-phpconfig - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ctw/ctw-middleware-phpconfig

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ctw/ctw-middleware-phpconfig
============================

This PSR-15 middleware allows php.ini settings to be set at runtime with values from a config file.

4.0.4(5mo ago)1163BSD-3-ClausePHPPHP ^8.3CI passing

Since Mar 13Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jonathanmaron/ctw-middleware-phpconfig)[ Packagist](https://packagist.org/packages/ctw/ctw-middleware-phpconfig)[ RSS](/packages/ctw-ctw-middleware-phpconfig/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (32)Used By (0)

Package "ctw/ctw-middleware-phpconfig"
======================================

[](#package-ctwctw-middleware-phpconfig)

[![Latest Stable Version](https://camo.githubusercontent.com/99bd7be85f24f3fc33fe40946ad3c12048b9aab102ea20f95e8bfa06f5adc341/68747470733a2f2f706f7365722e707567782e6f72672f6374772f6374772d6d6964646c65776172652d706870636f6e6669672f762f737461626c65)](https://packagist.org/packages/ctw/ctw-middleware-phpconfig)[![GitHub Actions](https://github.com/jonathanmaron/ctw-middleware-phpconfig/actions/workflows/tests.yml/badge.svg)](https://github.com/jonathanmaron/ctw-middleware-phpconfig/actions/workflows/tests.yml)[![Scrutinizer Build](https://camo.githubusercontent.com/bfde103c33812a915bd65eb29165d04357bb52f4882ba8040dc5244828882c70/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d706870636f6e6669672f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-phpconfig/build-status/master)[![Scrutinizer Quality](https://camo.githubusercontent.com/50ffe9327ce6d3679e33c721736b88e606bb0e3b60fa475134f9853eea55b599/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d706870636f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-phpconfig/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/7fe13207f236aad0f1b32fab8095785f3de91749da0686cd3d311a6ce9d51bc7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d706870636f6e6669672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-phpconfig/?branch=master)

PSR-15 middleware that sets PHP configuration options at runtime from application configuration files.

Introduction
------------

[](#introduction)

### Why This Library Exists

[](#why-this-library-exists)

PHP applications often require specific `php.ini` settings that differ from server defaults. While these can be set in the server's `php.ini` or `.htaccess` files, managing settings at the application level provides several advantages:

- **Version control**: Configuration settings travel with your application code
- **Environment flexibility**: Different settings for development, staging, and production
- **Shared hosting compatibility**: Works even when `php.ini` modifications are restricted
- **Centralized management**: All application configuration in one place
- **Runtime adaptability**: Settings can be computed or loaded from external sources

This middleware applies PHP configuration settings early in the request lifecycle, ensuring all subsequent code operates with the correct settings.

### Problems This Library Solves

[](#problems-this-library-solves)

1. **Server dependency**: Relying on server administrators to configure `php.ini` correctly
2. **Configuration drift**: Settings differ between environments without version control
3. **Shared hosting limits**: Many hosts restrict `php.ini` modifications
4. **Scattered configuration**: PHP settings in `.htaccess`, `php.ini`, and code are hard to track
5. **Environment-specific needs**: Development needs different settings than production

### Where to Use This Library

[](#where-to-use-this-library)

- **Shared hosting environments**: Set configuration when server access is limited
- **Containerized applications**: Configure PHP settings as part of application bootstrap
- **Multi-environment deployments**: Different settings per environment via config files
- **Framework applications**: Integrate PHP configuration with Mezzio/Laminas config system
- **Development overrides**: Enable error display, increase memory limits during development

### Design Goals

[](#design-goals)

1. **Configuration-driven**: Settings come from application config arrays, not hardcoded
2. **Type normalization**: Converts booleans to `On`/`Off`, integers to strings automatically
3. **Fail-fast validation**: Throws exceptions for invalid or unchangeable options
4. **Early execution**: Apply settings before other middleware processes requests
5. **Transparent operation**: No modification to request or response

Requirements
------------

[](#requirements)

- PHP 8.3 or higher
- ctw/ctw-middleware ^4.0

Installation
------------

[](#installation)

Install by adding the package as a [Composer](https://getcomposer.org) requirement:

```
composer require ctw/ctw-middleware-phpconfig
```

Usage Examples
--------------

[](#usage-examples)

### Basic Pipeline Registration (Mezzio)

[](#basic-pipeline-registration-mezzio)

```
use Ctw\Middleware\PhpConfigMiddleware\PhpConfigMiddleware;

// In config/pipeline.php - place early in the pipeline
$app->pipe(PhpConfigMiddleware::class);
```

### ConfigProvider Registration

[](#configprovider-registration)

```
// config/config.php
return [
    // ...
    \Ctw\Middleware\PhpConfigMiddleware\ConfigProvider::class,
];
```

### Configuration File

[](#configuration-file)

```
// config/autoload/php-config.global.php
return [
    'php_config' => [
        'display_errors'         => false,
        'error_reporting'        => E_ALL,
        'max_execution_time'     => 30,
        'memory_limit'           => '256M',
        'post_max_size'          => '64M',
        'upload_max_filesize'    => '64M',
        'date.timezone'          => 'UTC',
        'session.cookie_secure'  => true,
        'session.cookie_httponly'=> true,
    ],
];
```

### Development Overrides

[](#development-overrides)

```
// config/autoload/php-config.local.php (git-ignored)
return [
    'php_config' => [
        'display_errors'    => true,
        'error_reporting'   => E_ALL,
        'memory_limit'      => '512M',
        'max_execution_time'=> 0, // No limit in development
    ],
];
```

### Type Conversion

[](#type-conversion)

The middleware automatically converts values to the correct format for `ini_set()`:

PHP TypeInputOutput`bool``true``'On'``bool``false``'Off'``int``256``'256'``string``'256M'``'256M'``null``null``''`### Common Configuration Options

[](#common-configuration-options)

OptionDescriptionExample`display_errors`Show errors in output`false` (production)`error_reporting`Error level bitmask`E_ALL``memory_limit`Maximum memory usage`'256M'``max_execution_time`Script timeout in seconds`30``upload_max_filesize`Maximum upload file size`'64M'``post_max_size`Maximum POST data size`'64M'``date.timezone`Default timezone`'UTC'``session.cookie_secure`HTTPS-only cookies`true``session.cookie_httponly`HTTP-only cookies`true`### Error Handling

[](#error-handling)

If a configuration option cannot be set (due to PHP restrictions or invalid option names), the middleware throws an `UnexpectedValueException`:

```
use Ctw\Middleware\PhpConfigMiddleware\Exception\UnexpectedValueException;

try {
    $app->run();
} catch (UnexpectedValueException $e) {
    // Handle configuration error
    error_log($e->getMessage());
}
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance71

Regular maintenance activity

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~57 days

Recently: every ~131 days

Total

31

Last Release

166d ago

Major Versions

1.0.1 → 2.0.02022-02-14

2.0.0 → 3.0.02022-07-07

3.0.22 → 4.0.02024-06-18

PHP version history (4 changes)1.0.0PHP ^7.4 || ^8.0

3.0.0PHP ^8.0

3.0.9PHP ^8.1

4.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/18d8bc9bdee8ab1b0cfccce6a06d2438acbed3a58b0046c4834c5678f6769342?d=identicon)[jonathanmaron](/maintainers/jonathanmaron)

---

Top Contributors

[![jonathanmaron](https://avatars.githubusercontent.com/u/298462?v=4)](https://github.com/jonathanmaron "jonathanmaron (53 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ctw-ctw-middleware-phpconfig/health.svg)

```
[![Health](https://phpackages.com/badges/ctw-ctw-middleware-phpconfig/health.svg)](https://phpackages.com/packages/ctw-ctw-middleware-phpconfig)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.4k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

704122.9M10.0k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31278.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k17](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
