php - Lumen - remove duplicate method -
in lumen have duplicated method 'responderror()' in base controller.php
class , handler.php
in exception folder.
to avoid duplication, suggest move method accessible classes?
public function responderror($errortype = '', $message = null, $statuscode = 500) { return response([ 'success' => false, 'error_type' => $errortype, 'errors' => [], 'message' => $message, ], $statuscode); }
you should move code own class:
class errorresponse { protected $errortype = null; protected $message = null; protected $statuscode = null; public __construct($errortype = '', $message = null, $statuscode = 500) { $this->errortype = $errortype; $this->message = $message; $this->statuscode = statuscode ; } public getresponse() { return response([ 'success' => false, 'error_type' => $this->errortype, 'errors' => [], 'message' => $this->message, ], $this->statuscode); } }
why should add overhead ask? may find in future, want add details response, e.g. debugging traceback of exception, etc. or might want return response function: typical situation: controller calls api function, api function gives low level error, controller needs add details it. using framework error class this, make whole api dependent on framework, should avoid best can.
you can use code everywhere:
$error = new errorresponse('bad error', 'something went wrong!'); return $error->getresponse();
Comments
Post a Comment