Skip to content

Transform your Yii2 applications into high-performance, PSR-compliant powerhouses.

License

Notifications You must be signed in to change notification settings

yii2-extensions/psr-bridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

239 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Yii Framework

PSR bridge


PHPUnit Mutation Testing PHPStan

Transform your Yii2 applications into high-performance, PSR-compliant powerhouses
Supporting traditional SAPI, RoadRunner, FrankenPHP, and worker-based architectures

Available deployment options

High-Performance Worker Mode

Long-running PHP workers for higher throughput and lower latency.

FrankenPHP RoadRunner

Features

Feature Overview

Installation

composer require yii2-extensions/psr-bridge:^0.3

Quick start

Worker mode (FrankenPHP)

<?php

declare(strict_types=1);

// disable PHP automatic session cookie handling
ini_set('session.use_cookies', '0');

require_once dirname(__DIR__) . '/vendor/autoload.php';

use yii2\extensions\frankenphp\FrankenPHP;
use yii2\extensions\psrbridge\http\Application;

// Load environment variables from .env file
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
$dotenv->safeLoad();

// production default (change to 'true' for development)
define('YII_DEBUG', $_ENV['YII_DEBUG'] ?? false);
// production default (change to 'dev' for development)
define('YII_ENV', $_ENV['YII_ENV'] ?? 'prod');

require_once dirname(__DIR__) . '/vendor/yiisoft/yii2/Yii.php';

$config = require_once dirname(__DIR__) . '/config/web/app.php';

$app = new Application($config);
$runner = new FrankenPHP($app);

$runner->run();

Worker mode (RoadRunner)

<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use yii2\extensions\psrbridge\http\Application;
use yii2\extensions\roadrunner\RoadRunner;

define('YII_DEBUG', getenv('YII_DEBUG') ?? false);
define('YII_ENV', getenv('YII_ENV') ?? 'prod');

require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require dirname(__DIR__) . '/config/web/app.php';

$app = new Application($config);
$runner = new RoadRunner($app);

$runner->run();

PSR-7 Conversion

// Convert Yii2 request to PSR-7
$request = Yii::$app->request;
$psr7Request = $request->getPsr7Request();

// Convert Yii2 response to PSR-7
$response = Yii::$app->response;
$psr7Response = $response->getPsr7Response();

// Emit PSR-7 response
$emitter = new yii2\extensions\psrbridge\emitter\SapiEmitter();
$emitter->emit($psr7Response);

Worker lifecycle defaults

In long-running workers, keep Application lifecycle defaults unless you have a specific requirement:

  • useSession=true
  • syncCookieValidation=true
  • resetUploadedFiles=true

Warning

Keep request-scoped components (request, response, errorHandler, session, user) out of Application::$persistentComponents.

Components listed in Application::$persistentComponents (defaults to db and cache) keep loaded instances across requests.

Define lifecycle flags before run() (via config or property setters).

$config = [
    'class' => Application::class,
    // disable session and cookie validation sync for stateless REST APIs; keep resetUploadedFiles=true (the default)
    // unless you have a specific reason to retain uploaded file state across requests
    'useSession' => false,
    'syncCookieValidation' => false,
    'resetUploadedFiles' => true,
];

Smart Body Parsing

The bridge automatically parses incoming PSR-7 request bodies based on the Content-Type header and your configured parsers (for example, application/json), ensuring Yii::$app->request->post() works seamlessly in worker mode without extra boilerplate.

Documentation

For detailed configuration options and advanced usage.

Package information

PHP Yii 2.0.x Yii 2.0.x Latest Stable Version Total Downloads

Quality code

Codecov PHPStan Level Max Super-Linter StyleCI

Our social networks

Follow on X

License

License

About

Transform your Yii2 applications into high-performance, PSR-compliant powerhouses.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages