-
-
Save majirieyowel/0f74183ae2c11e6876b8055328267557 to your computer and use it in GitHub Desktop.
Custom error format response Lumen Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Laravel\Lumen\Routing\Controller as BaseController; | |
class Controller extends BaseController | |
{ | |
public function __construct() | |
{ | |
$this->customErrorFormat(); | |
} | |
/** | |
* Custom error response format | |
* | |
* @return Illuminate\Http\Response | |
*/ | |
private function customErrorFormat() | |
{ | |
static::$errorFormatter = function ($validator) { | |
$arr = []; | |
foreach ($validator->errors()->toArray() as $key => $value) { | |
$arr[$key] = $value[0]; | |
} | |
return [ | |
'errors' => $arr, | |
'meta' => [ | |
'code' => 422, | |
'message' => 'VALIDATION_FAILED', | |
], | |
]; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment