FrankenPHP Support

Hello!
Recently PHPFoundation announced that FrankenPHP is supported by the organization. Currently CakePHP is supported to work with FrankenPHP, if not, are there any plans to include this support? I imagine this will be the default production-ready web server for PHP soon.

2 Likes

Tried this. The worker script seems to make it a bit quicker as it doesn’t have to Bootstrap on each request.

Did find that I had to specify extra workers as the Docker image seems to launch only one by default and I have Browsershot making requests back to the server to generate PDF’s

Caddy / FrankenPHP seems like a drop-in replacement

I am pleasantly surprised that the worker mode just works and doesn’t require any changes in the bootstrapping process.

I thought to have the worker mode work you need a custom start script like Worker configuration example for CakePHP ^4 · Issue #1464 · dunglas/frankenphp · GitHub

could you maybe provide a documentation on how to get cake working with worker mode?

I think I’m not understanding something with how to deploy worker mode.

Caddy / FrankenPHP works out of the box with webroot/index.php being called. Still not sure how to get the worker mode running

So when you replace webroot/index.php with the worker.php script it errors out

Fatal error: Uncaught Cake\Http\Exception\MissingControllerException: Controller class `` could not be found. in /var/www/html/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:335
Stack trace: 
#0 /var/www/html/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php(320): Cake\Controller\ControllerFactory->missingController() 
#1 /var/www/html/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php(75): Cake\Controller\ControllerFactory->getControllerClass() 
#2 /var/www/html/vendor/cakephp/cakephp/src/Http/BaseApplication.php(360): Cake\Controller\ControllerFactory->create() 
#3 /var/www/html/webroot/index.php(20): Cake\Http\BaseApplication->handle() 
#4 [internal function]: {closure:/var/www/html/webroot/index.php:17}() 
#5 /var/www/html/webroot/index.php(28): frankenphp_handle_request() 
#6 {main} thrown in /var/www/html/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php on line 335

I’ve managed to get it running as a worker.

I don’t think the sample code from github can work because it doesn’t consider middleware and events. Middleware is crucial for a lot of things.

I had to copy vendor/cakephp/cakephp/src/Http/Server.php to src/Http and modify it to remove the call to $this->bootstrap() (so Application can be bootstrapped once instead of for each request).

More details here => FrankenPHP + Caddy + Ubuntu 24.04 + CakePHP | Toggen

<?php

use App\Application;
use App\Http\Server;
use Cake\Core\PluginApplicationInterface;
$workerServer = $_SERVER;

ignore_user_abort(true);

require  dirname(__DIR__) . '/vendor/autoload.php';
$myApp = new Application( dirname(__DIR__)  . '/config');
$myApp->bootstrap();
# $myApp->pluginBootstrap();

$server = new Server($myApp);

error_log('Worker started');

$handler = static function () use ($server, $myApp)  {
        error_log('Worker handler');
        # DebugKit won't show up without this if statement
        # or if you move it out of the closure
        if ($myApp instanceof PluginApplicationInterface) {
                $myApp->pluginBootstrap();
        }
        $server->emit($server->run());
};

$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 2);

for ($nbRequests = 0; !$maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
    error_log('Worker handling request');
    $keepRunning = \frankenphp_handle_request($handler);

    gc_collect_cycles();

    if (!$keepRunning) break;
}