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

370
Vistas
Using json's value as slim's error code

Right now I have this:

$error = new Error($id, $from, $to);
return $response
    ->withStatus(422)
    ->withHeader('Content-Type', 'text/html')
    ->write($error->error());

The Error class returns this:

{"error":true,"code":422,"text":"Blah!"}

How can I set the error code in ->withStatus(422) so it's set by the json string instead of by me manually?

Edit: I guess this could work:

$code = json_decode($error->error(), true);
$code = $code['code'];
/* ...blah blah code... */
->withStatus($code)

But I would like to know if there's a way to do it with less code.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Update the Error class so that it has a code() public method that returns just the 422. You can now do:

$error = new Error($id, $from, $to);
return $response
    ->withStatus($error->code())
    ->withHeader('Content-Type', 'application/json')
    ->write($error->error());

Incidentally, as you're using JSON, if you make error() return an array, you can do this:

$error = new Error($id, $from, $to);
return $response->withJson($error->error(), $error->code());
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