DEV Community

Sergey Telpuk
Sergey Telpuk

Posted on

Roadrunner & Zend Expressive & Cycle ORM. Not allow to php to die.

Hello everyone! This article is dedicated to run PHP-application under Roadrunner(RR). if you haven't gotten acquainted with RR yet, I put forward to visiting the link.
In my past article, I tried to describe how easy to integrate Cycle ORM into ZE, but it isn't so interesting like setting up the following stack: Roadrunner, Zend Expressive, Cycle ORM. Maybe It'll give you another way of starting off your new application with that one.
ZE isn't so popular among PHP developers. From my point of view It's underestimated micro-framework because of that there was chosen ZE. So, go ahead!

I don't like to get a deal with setting local-env, so I give my preference in favor of docker. I prepared the repository for running RR with the help of docker-compose, It can be found by the following link.

After cloned roadrunner-docker-skeleton. We can use Makefile for building and running.
For the first step, run Makefile command:

make build

Note: don't forget copy sample.env into .env.

For the next step, should install ZE skeleton application:

docker-compose run composer create-project zendframework/zend-expressive-skeleton expressive  

For the next step, change file expressive/public/index.php:

<?php declare(strict_types=1);

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

return (function () {
    $container = require 'config/container.php';
    $app = $container->get(\Zend\Expressive\Application::class);
    $factory = $container->get(\Zend\Expressive\MiddlewareFactory::class);
    (require 'config/pipeline.php')($app, $factory, $container);
    (require 'config/routes.php')($app, $factory, $container);

    return $app;
})();

For the next step, change file worker.php:

<?php declare(strict_types=1);

ini_set('display_errors', 'stderr');

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

$app = require 'expressive/public/index.php';

$relay = new Spiral\Goridge\StreamRelay(STDIN, STDOUT);
$psr7 = new Spiral\RoadRunner\PSR7Client(new Spiral\RoadRunner\Worker($relay));

while ($req = $psr7->acceptRequest()) {
    try {
        $response = $app->handle($req);
        $psr7->respond($response);
    } catch (\Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

For using Cycle ORM under ZE to follow the link.
Finish step is to run our RR:

make up

Enjoy, http://localhost:8080

Top comments (0)