Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

465
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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);
    }
});
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!