简体   繁体   中英

How to do a classic php exit() on swoole?

For example this code

<?php
if (some_condition()) {
    header('Location: /');
    exit();
}

// do a lot of things...

On classic PHP FPM previous code works fine. But on PHP+Swoole, we ge this error

swoole exit {"exception":"[object] (Swoole\\ExitException(code: 0): swoole exit at

The error is understandable. But, what is the easiest way to migrate it?

For PHP FPM, each piece of code was executed individually, and the result (output of that process) was piped back to a client. So whenever we wanted to stop processing, we just triggered exit() .

With Swoole , however, the server is expected to run all the time. Sure, it's possible to stop the Process with Swoole\\Process::exit() - but it's usually up to controller's to trigger sending the response immediately. For example:

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);

$server->on("start", function (Swoole\Http\Server $server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$server->start();

In this case, $response->end is the method that does essentially the same thing as exit() in PHP FPM world. Note that it's quite similar to what happens in Node world: the server is expected to be running all the time, and it's the controllers (functions processing each individual request) deciding whether to stop handling individual request and pass back the response, both headers and body.

How to replace exit() on Laravel + Swoole

Yes, its a very bad practice exit() function. But own Laravel app bootstrap a Legacy code. Then, by associated costs, its a better solution this way.

  1. Do a renderable exception
class ExitException extends Exception implements Renderable
{
    public function report() {
        return true; // dont report
    }

    public function render()
    {
        return ' '; // prevent 500 html dump error
    }
}
  1. Replace exit(); with
// exit;
throw new ExitException();

That exception will be handled by your app Error Handler, and send an empty string as response to Swoole request handler.

I repeat: IS NOT AN ELEGANT SOLUTION, just prevent change a lot of code on legacy app.

You can learn about \\Swoole\\ExitException .

use Swoole\Coroutine;
use function Swoole\Coroutine\run;

function route()
{
    controller();
}

function controller()
{
    your_code();
}

function your_code()
{
    Coroutine::sleep(.001);
    exit(1);
}

run(function () {
    try {
        route();
    } catch (\Swoole\ExitException $e) {
        var_dump($e->getMessage());
        var_dump($e->getStatus() === 1);
        var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM