RoadRunner HTTP

RoadRunner is a complete PHP server, we can integrate it with Burner-Core without writing any special logic. If you choose RoadRunner as your server solution, you will hardly notice it in development.

The key part of worker.php is as follows:

while (true) {
    // get psr7 request
    try {
        $request = $psr7->waitRequest();
        if (! ($request instanceof RequestInterface)) { // Termination request received
            break;
        }
    } catch (Exception $e) {
        $psr7->respond(new Response(400)); // Bad Request

        continue;
    }

    /** @var \Psr\Http\Message\ResponseInterface */
    $response = \Monken\CIBurner\App::run($request);

    // handle response object
    try {
        $psr7->respond($response);
        Events::trigger('burnerAfterSendResponse', $psr7);
        \Monken\CIBurner\App::clean();
    } catch (Exception $e) {
        $psr7->respond(new Response(500, [], 'Something Went Wrong!'));
    }
}

All HTTP requests start from the waitRequest method, when this method gets the latest user request, it will immediately execute the App::run() method, and Burner will start a series of preprocessing and notify CodeIgniter4 to handle your connection.

Next, you will enter the CodeIgniter4 processing process, when CodeIgniter4 processing is complete, the run() method will return a PSR-7 response object, and transfer it to the respond method for processing.

After understanding the above process, you will find that the implementation logic of RoadRunner is not complicated, just like you are familiar with, develop your application as usual.

Events

CodeIgniter4 provides events that can be used to perform actions at specific points during execution. When CodeIgniter runs, it follows a certain execution order, but in some cases, you may want to perform some actions at a specific stage of execution.

Burner provides the following events for OpenSwoole HTTP:

burnerAfterSendResponse

This is usually used to initialize after the response, if you have some global variables that need to be cleared or some common processing after the response, it is very suitable to declare it in this event. You can declare the burnerAfterSendResponse event, and when OpenSwoole responds, the logic you need to execute will be executed:

// This code will be executed after sending the response to the client.
Events::on('burnerAfterSendResponse',static function(\Spiral\RoadRunner\Http\PSR7Worker $psr7Worker)
{
    //Your logic
});

The way events are used is not limited to callback functions. Please refer to the methods mentioned in the CodeIgniter4 User Guide to choose the best way for you.

Configuration

The automatically generated configuration file of Burner will be located in the root directory of the project under rr.yml, and its content may be as follows:

version: "2.7"

rpc:
  listen: tcp://127.0.0.1:6001

server:
  command: "php Worker.php -f=/app/CodeIgniter4-Burner/src/FrontLoader.php -a=/app/CodeIgniter4-Burner/dev/app/"
  # env:
  #   XDEBUG_SESSION: 1

http:
  address: "0.0.0.0:8080"
  static:
    dir: "/app/CodeIgniter4-Burner/dev/public"
    forbid: [".htaccess", ".php"]
  pool:
    num_workers: 1
    # max_jobs: 64
    # debug: true

# reload:
#   interval: 1s
#   patterns: [ ".php" ]
#   services:
#     http:
#       recursive: true
#       ignore: [ "vendor" ]
#       patterns: [ ".php", ".go", ".dmd" ]
#       dirs: [ "/app/CodeIgniter4-Burner/dev" ]

# logs:
#   mode: development
#   output: stdout
#   file_logger_options:
#     log_output: "/app/CodeIgniter4-Burner/dev/writable/logs/RoadRunner.log"
#     max_size: 100
#     max_age: 1
#     max_backups : 5
#     compress: false

You may find that in the configuration of server.command, we use php Worker.php -f -a to start the RoadRunner Worker, this is because we need to load the CodeIgniter4 core in the Worker, and this core needs to be handled by the FrontLoader provided by Burner.

At the same time, the location of the app folder is also very important for CodeIgniter4, so we need to pass these two parameters when starting the Worker, and these two parameters will be automatically generated by the Burner initialization command.

You can refer to the official full version document and official user manual according to your own needs to modify this file.

Powered by Doctave