Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

467
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda