/** * Bootstrap the application services. * * @return void */ public function boot(ResponseFactory $factory) { $factory->macro('validation_error', function ($messageBag, $defaultFullErrorMessage = null) use($factory) { $errorId = 'validation_error'; $jsonResponse = ["id" => $errorId, "fields" => []]; $fullErrorMessage = $messageBag->get('full_error_message'); if ($fullErrorMessage) { $jsonResponse['message'] = $fullErrorMessage[0]; } else { if ($defaultFullErrorMessage != null) { $jsonResponse['message'] = $defaultFullErrorMessage; } else { $jsonResponse['message'] = 'Los datos enviados contienen errores'; } } foreach ($messageBag->getMessages() as $key => $fieldMessages) { if (strcmp($key, 'full_error_message') != 0) { $jsonResponse["fields"][] = [$key => $fieldMessages[0]]; } } return $factory->make($jsonResponse, 400); }); $factory->macro('not_found', function ($message) use($factory) { $errorId = 'not_found'; $jsonResponse = ["id" => $errorId, "message" => $message]; return $factory->make($jsonResponse, 404); }); }
/** * Bootstrap the application services. * * @param ResponseFactory $factory */ public function boot(ResponseFactory $factory) { $factory->macro('error', function ($code, $msg) use($factory) { return $factory->json(array('success' => false, 'code' => $code, 'msg' => $msg)); }); $factory->macro('success', function ($data = null) use($factory) { if ($data === null) { return $factory->json(array('success' => true)); } return $factory->json(array('success' => true, 'data' => $data)); }); }
/** * Bootstrap the application services. * * @return void */ public function boot(ResponseFactory $factory) { // macroメソッドは登録名を第1引数、クロージャーを第2引数に取ります。 // マクロのクロージャーはResponseFactoryの実装か、responseヘルパーに対し、登録名で呼び出すことで、実行されます。 // return response()->caps('foo'); $factory->macro('çsv', function ($value) use($factory) { // CSV download 処理を追加する }); }
/** * Perform post-registration booting of services. * * @param ResponseFactory $factory * @return void */ public function boot(ResponseFactory $factory) { $factory->macro('calculatePercent', function ($num_amount, $num_total) use($factory) { if ($num_total == 0) { return '0%'; } $count1 = $num_amount / $num_total; $count2 = $count1 * 100; $count = number_format($count2, 0); return $count . ' %'; }); }
/** * Bootstrap the application services. */ public function boot(ResponseFactory $factory, Request $request) { $factory->macro('yaml', function ($value, $status = 200, array $headers = [], $style = YamlResponse::MULTILINE_YAML) use($factory) { YamlResponse::prepareHeaders($headers); return $factory->make(YamlResponse::dump($value, $style), $status, $headers); }); $request->macro('wantsYaml', function () use($request) { return YamlRequest::wantsYaml($request); }); $request->macro('isYaml', function () use($request) { return YamlRequest::isYaml($request); }); }
/** * Perform post-registration booting of services. * * @param ResponseFactory $response * @return void */ public function boot(ResponseFactory $response) { $response->macro('crossDomainJson', function ($value) use($response) { return $response->json($value)->header('Access-Control-Allow-Origin', '*'); }); }
/** * Bootstrap the application services. * * @return void */ public function boot(ResponseFactory $factory) { $factory->macro('caps', function ($value) { }); }
/** * Bootstrap the application services. * * @return void */ public function boot(ResponseFactory $factory) { $factory->macro('api', function ($httpStatus, $outcome, $error, $body) use($factory) { return $factory->json(['header' => ['success' => $outcome, 'msg' => $error], 'body' => [$body]], $httpStatus); }); }