/** * 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); }); }
/** * @param string $middleware * @param bool $isAjax * @return \Illuminate\Http\RedirectResponse */ protected function failedResponse($middleware, $isAjax = false) { if ($isAjax) { return $this->response->make($this->getErrorMessage($middleware), 403); } return $this->redirector->back()->exceptInput('_guard_pot', '_guard_opened')->withErrors($this->getErrorMessage($middleware)); }
/** * @inheritdoc */ public function make($data, $statusCode = 200, array $headers = []) { $acceptedMediaType = $this->requestParser->acceptedMediaType(); $mediaType = $this->versionFactory->makeMediaType($acceptedMediaType); $representation = $mediaType->format($data); $headers = $this->setContentTypeHeader($headers, $acceptedMediaType); return $this->response->make($representation, $statusCode, $headers); }
/** * Run the request filter. * * @param \Illuminate\Routing\Route $route * @param \Illuminate\Http\Request $request * @return mixed */ public function filter(Route $route, Request $request) { if ($this->auth->guest()) { if ($request->ajax()) { return $this->response->make('Unauthorized', 401); } else { return $this->response->redirectGuest('auth/login'); } } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($this->auth->guest()) { if ($request->ajax()) { return $this->response->make('Unauthorized', 401); } else { return $this->response->redirectGuest('auth/login'); } } return $next($request); }
/** * Response on authorized request. * * @param \Illuminate\Http\Request $request * * @return mixed */ protected function responseOnUnauthorized($request) { if ($request->ajax()) { return $this->response->make('Unauthorized', 401); } $type = $this->auth->guest() ? 'guest' : 'user'; $url = $this->config->get("orchestra/foundation::routes.{$type}"); return $this->response->redirectTo($this->foundation->handles($url)); }
/** * Importer class. * * @return \Symfony\Component\HttpFoundation\Response */ public function import() { $index = 0; $batch = []; foreach ($this->medicalCenterRepository->all() as $index => $medCenter) { $email = $medCenter->email; if ($email === '') { continue; } $batch[] = ['email' => compact('email'), 'merge_vars' => ['TOKEN' => $medCenter->token, 'NAME' => $medCenter->name, 'COUNTRY' => $medCenter->country, 'EMAIL' => $medCenter->email]]; } // Subscribe to Newsletter $results = $this->newsletter->batchSubscribeTo('TheseEcho', $batch, false, true); // Errors if ($results['error_count']) { return $this->response->make('There are errors ' . print_r($results['errors']) . '.', 405); } // Success return $this->response->make("Subscribed {$index} centers.", 200); }
/** * Authorize the user * * @param ResponseFactory $response * @return array|\Illuminate\Http\Response */ public function authorize(ResponseFactory $response) { $credentials = array_merge($this->request->only(['username', 'password']), ['status' => 'active']); if (!$this->auth->once($credentials)) { return $response->make('Invalid credentials', 401); } /* if (!$this->isAuthorized($userRoles)) { return $response->make('Unauthorized user', 401); }*/ return ['token' => $this->getUserToken($this->auth->user())]; }
/** * receiveRefund. * * @method receiveRefund * * @param \Illuminate\Http\Request $request * @param string $payumToken * * @return mixed */ public function receiveRefund(Request $request, $payumToken) { return $this->send($request, $payumToken, function ($gateway, $token, $httpRequestVerifier) { $gateway->execute(new Refund($token)); $httpRequestVerifier->invalidate($token); $afterUrl = $token->getAfterUrl(); if (empty($afterUrl) === false) { return $this->responseFactory->redirectTo($afterUrl); } return $this->responseFactory->make(null, 204); }); }
/** * 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); }); }
/** * dispatchAssets. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @return \Symfony\Component\HttpFoundation\Response */ protected function dispatchAssets($request, $next) { $assets = $request->get('_tracy_bar'); switch ($assets) { case 'css': $content = $this->debugbar->dispatchAssets(); $headers = ['content-type' => 'text/css; charset=utf-8', 'cache-control' => 'max-age=86400']; break; case 'js': case 'assets': $content = $this->debugbar->dispatchAssets(); $headers = ['content-type' => 'text/javascript; charset=utf-8', 'cache-control' => 'max-age=86400']; break; default: $this->storeWrapper->start(); $this->storeWrapper->restore(); $content = $this->debugbar->dispatchContent(); $headers = ['content-type' => 'text/javascript; charset=utf-8']; $this->storeWrapper->clean($assets)->close(); break; } return $this->responseFactory->make($content, 200, array_merge($headers, ['content-length' => strlen($content)])); }
function it_makes_a_response(\LaraPackage\Api\Contracts\Request\Parser $requestParser, ResponseFactory $response, \LaraPackage\Api\Contracts\Factory\VersionFactory $versionFactory, \LaraPackage\Api\Contracts\MediaType\Json $media, \Illuminate\Http\Response $illuminateResponse, \LaraPackage\Api\Contracts\Config\ApiVersion $versionInfoRetriever) { $mediaType = 'json'; $version = 4; $vendor = 'vnd.wps_api.'; $versionDesignator = 'v4'; $dataArray = ['data' => 'array']; $jsonData = json_encode($dataArray); $requestParser->acceptedMediaType()->shouldBeCalled()->willReturn($mediaType); $versionInfoRetriever->vendor($version)->shouldBeCalled()->willReturn($vendor); $versionInfoRetriever->versionDesignator($version)->shouldBeCalled()->willReturn($versionDesignator); $versionFactory->makeMediaType($mediaType)->shouldBeCalled()->willReturn($media); $media->format($dataArray)->shouldBeCalled()->willReturn($jsonData); $response->make($jsonData, 200, ['Content-Type' => 'application/' . $vendor . $versionDesignator . '+' . $mediaType])->shouldBeCalled()->willReturn($illuminateResponse); $this->make($dataArray)->shouldReturn($illuminateResponse); }
/** * media. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Filesystem\Filesystem $filesystem * @param \Illuminate\Contracts\Routing\ResponseFactory $request * @param string $file * * @return \Illuminate\Http\Response */ public function media(Request $request, Filesystem $filesystem, ResponseFactory $responseFactory, $file) { $filename = __DIR__ . '/../../../public/' . $file; $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript'; $lastModified = $filesystem->lastModified($filename); $eTag = sha1_file($filename); $headers = ['content-type' => $mimeType, 'last-modified' => date('D, d M Y H:i:s ', $lastModified) . 'GMT']; if (@strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified || trim($request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag) { $response = $responseFactory->make(null, 304, $headers); } else { $response = $responseFactory->stream(function () use($filename) { $out = fopen('php://output', 'wb'); $file = fopen($filename, 'rb'); stream_copy_to_stream($file, $out, filesize($filename)); fclose($out); fclose($file); }, 200, $headers); } return $response->setEtag($eTag); }
/** * 以客户端需求自动转换响应格式 * * @param array $data * @param string $message * @param int $code * @param null|string $format * * @return Response */ public function make($data = array(), $message = '', $code = 0, $format = null) { !empty($data) && $this->setData($data); !empty($message) && $this->setMessage($message); $code && $this->setCode($code); !empty($format) && $this->setFormat($format); switch ($this->getFormat() ?: $this->request->format()) { case 'xml': return $this->response->make($this->toXML())->header('Content-Type', 'application/xml'); break; case 'html': return $this->response->make($this->getMessage()); break; case 'jsonp': $this->response->setCallback($this->getCallback()); case 'json': default: return $this->response->json($this->toArray()); break; } }
/** * Render response with format. * * @param string $format * @return mixed */ public function render($format = null, $callback = null) { $format = $format ?: $this->format; $returned = $this->returned; // Change error code number. if ($this->code and isset($returned['response']['code'])) { $returned['response']['code'] = $this->code; } switch ($format) { case 'xml': if (isset($returned['response']['data'])) { $returned['response']['entries'] = $returned['response']['data']; unset($returned['response']['data']); } $data = array('type' => 'application/xml', 'content' => $this->converter->factory($returned['response'])->toXML()); break; case 'php': $data = array('type' => 'text/plain', 'content' => $this->converter->factory($returned['response'])->toPHP()); break; case 'serialized': $data = array('type' => 'text/plain', 'content' => $this->converter->factory($returned['response'])->toSerialized()); break; // In case JSON we don't need to convert anything. // In case JSON we don't need to convert anything. case 'json': default: $response = $this->response->json($returned['response'], $returned['header']); // Support JSONP Response. if ($callback) { $response = $response->setCallback($callback); } return $response; break; } // Making response. $response = $this->response->make($data['content'], $returned['header']); //$response = response() $response->header('Content-Type', $data['type']); return $response; }
/** * Handle the command. * * @param SettingRepositoryInterface $settings * @param ResponseFactory $response * @param Factory $view * @return \Symfony\Component\HttpFoundation\Response */ public function handle(SettingRepositoryInterface $settings, ResponseFactory $response, Factory $view) { $lockoutInterval = 1; return $response->make($view->make('minioak.extension.twofactor::twofa', []), 200)->setTtl($lockoutInterval * 1); }
/** * Handle the command. * * @param SettingRepositoryInterface $settings * @param ResponseFactory $response * @param Factory $view * @return \Symfony\Component\HttpFoundation\Response */ public function handle(SettingRepositoryInterface $settings, ResponseFactory $response, Factory $view) { $lockoutInterval = $settings->value('anomaly.extension.throttle_security_check::lockout_interval', 1); return $response->make($view->make('streams::errors/429', []), 429)->setTtl($lockoutInterval * 1); }
/** * sendStreamedResponse. * * @method sendStreamedResponse * * @param string $content * * @return \Symfony\Component\HttpFoundation\Response */ protected function sendStreamedResponse($content, $headers) { return $this->responseFactory->make($content, 200, $headers); }
/** * Render binary file * * @param string $binaryFile * @return Response */ protected function renderBinaryFile($binaryFile) { $status = 200; return $this->responseFactory->make($binaryFile, $status, $this->getHeaders()); }