facebook twitter hatena line email

「Php/laravel/laravel5/exception」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==404や403や500をカスタマイズする場合== app/Exceptions/Handler.php public function render($request, Exception $exception) { if ($this->isHttpExcept...」)
 
(その他パターンはこちらに書いてある)
行26: 行26:
 
  }
 
  }
  
==その他パターンはこちらに書いてある==
+
==responseのメソッドはこちらに書いてある==
 
vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php
 
vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php
  make(
+
  make($content = '', $status = 200, array $headers = [])
  view(
+
  view($view, $data = [], $status = 200, array $headers = [])
  json(
+
  json($data = [], $status = 200, array $headers = [], $options = 0)
  jsonp(
+
  jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
  stream(
+
  stream($callback, $status = 200, array $headers = [])
  download(
+
  download($file, $name = null, array $headers = [], $disposition = 'attachment')
  redirectTo(
+
  redirectTo($path, $status = 302, $headers = [], $secure = null)
  redirectToRoute(
+
  redirectToRoute($route, $parameters = [], $status = 302, $headers = [])
  redirectToAction(
+
  redirectToAction($action, $parameters = [], $status = 302, $headers = [])
  redirectGuest(
+
  redirectGuest($path, $status = 302, $headers = [], $secure = null)
  redirectToIntended(
+
  redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)

2018年2月16日 (金) 10:01時点における版

404や403や500をカスタマイズする場合

app/Exceptions/Handler.php

public function render($request, Exception $exception)
{
       if ($this->isHttpException($exception)) {
           if ($exception->getStatusCode() == 404) {
               return response()->make("404");
           } elseif ($exception->getStatusCode() == 500) {
               return response()->make("500");
           }
       }
   return parent::render($request, $exception);
}

jsonにする場合

app/Exceptions/Handler.php

return response()->json([
       'status' => 500,
       'errors' => $this->getMessage(500)
   ], 500);

viewに渡す場合

app/Exceptions/Handler.php

if($e->getStatusCode() == 403) {
     return response()->view('errors.403');
}

responseのメソッドはこちらに書いてある

vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php

make($content = , $status = 200, array $headers = [])
view($view, $data = [], $status = 200, array $headers = [])
json($data = [], $status = 200, array $headers = [], $options = 0)
jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
stream($callback, $status = 200, array $headers = [])
download($file, $name = null, array $headers = [], $disposition = 'attachment')
redirectTo($path, $status = 302, $headers = [], $secure = null)
redirectToRoute($route, $parameters = [], $status = 302, $headers = [])
redirectToAction($action, $parameters = [], $status = 302, $headers = [])
redirectGuest($path, $status = 302, $headers = [], $secure = null)
redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)