Slide 1

Slide 1 text

Τϥʔ࣌ʹϩάʹग़ྗ͢Δ৘ใ ͱը໘ʹදࣔ͢Δ৘ใΛ෼͚Δ okashoi@Laravel Meetup Tokyo Vol.12

Slide 2

Slide 2 text

͓·͑ͩΕʁ • Ԭా ਖ਼ฏʗ͓͔͠ΐ͍ • גࣜձࣾ΢Οϧήʔτ • ΞʔΩςΫτ ݉ ٕज़޿ใ ← New!! • ٕज़ॻయ 6 Ͱٕज़ಉਓࢽσϏϡʔͨ͠ • ͜ͷ LT ͸ͦͷ 1 খઅΑΓ • ൓Ԡ͋Δͱخ͍͠Ͱ͢🙇 #LaravelTokyo !2

Slide 3

Slide 3 text

΍Γ͍ͨ͜ͱ !3

Slide 4

Slide 4 text

΍Γ͍ͨ͜ͱ !4 Τϥʔʢ˞ʣൃੜ࣌ ˞ΞϓϦέʔγϣϯίʔυͰϋϯυϦϯά͞Εͳ͔ͬͨྫ֎

Slide 5

Slide 5 text

΍Γ͍ͨ͜ͱ !5 Τϥʔʢ˞ʣൃੜ࣌ ϨεϙϯεͱϩάͷϝοηʔδΛ
 ͦΕͧΕҟͳΔ΋ͷʹ͍ͨ͠ ˞ΞϓϦέʔγϣϯίʔυͰϋϯυϦϯά͞Εͳ͔ͬͨྫ֎

Slide 6

Slide 6 text

ϕʔεͱͳͬͨΞΠσΞ https://speakerdeck.com/suzuken/phpcon2017 !6

Slide 7

Slide 7 text

ͳΜͰ΍Γ͍ͨͷʁ • Ϣʔβ޲͚ը໘ʹγεςϜʹؔ͢ΔϝοηʔδΛදࣔͯ͠͠·͏ • Ϣʔβͷؔ৺͝ͱͰ͸ͳ͍ʗແ༻ͳࠞཚΛੜΉ • ηΩϡϦςΟతͳϦεΫ͕͋Δ • ϩάʹϢʔβ޲͚ϝοηʔδ͔͠ग़ྗ͞Ε͍ͯͳ͍ • ໰୊Λղফ͢Δख͕͔ΓʹͳΒͳ͍ʢʮΤϥʔ͕ൃੜ͠·ͨ͠ʯʣ !7

Slide 8

Slide 8 text

࣮ݱํ๏Λͻͱ͜ͱͰ·ͱΊΔͱ ಠࣗྫ֎ʹɺࣗ਎Λ HttpException ΁ม׵͢ΔϝιουΛͭ͘Δͱָ😄 !8

Slide 9

Slide 9 text

ΞδΣϯμ • ΍Γ͍ͨ͜ͱ ←DONE • σϑΥϧτͷΤϥʔϋϯυϦϯάͷڍಈ • ࣮ݱख๏ !9

Slide 10

Slide 10 text

ΞδΣϯμ • ΍Γ͍ͨ͜ͱ • σϑΥϧτͷΤϥʔϋϯυϦϯάͷڍಈ • ࣮ݱख๏ !10

Slide 11

Slide 11 text

• App\Exceptions\Handler ʹྫ֎ϋϯυϦϯάॲཧ͕ॻ͚Δ • ϩΪϯάʹؔ͢Δڍಈ report() • HTTP Ϩεϙϯεʹؔ͢Δڍಈ render() • جఈΫϥε͸ Illuminate\Foundation\Exceptions\Handler

Slide 12

Slide 12 text

• App\Exceptions\Handler ʹྫ֎ϋϯυϦϯάॲཧ͕ॻ͚Δ • ϩΪϯάʹؔ͢Δڍಈ report() • HTTP Ϩεϙϯεʹؔ͢Δڍಈ render() • جఈΫϥε͸ Illuminate\Foundation\Exceptions\Handler

Slide 13

Slide 13 text

Illuminate\Foundation\Exceptions\Handler::render() !13 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); }

Slide 14

Slide 14 text

Illuminate\Foundation\Exceptions\Handler::render() !14 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ྫ֎ࣗ৴͕ render ՄೳͳΒ༏ઌతʹͦΕΛར༻

Slide 15

Slide 15 text

Illuminate\Foundation\Exceptions\Handler::render() !15 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ಛఆͷྫ֎Λผͷྫ֎ʹม׵

Slide 16

Slide 16 text

Illuminate\Foundation\Exceptions\Handler::render() !16 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ಛఆͷྫ֎ʹ͍ͭͯઐ༻ͷॲཧΛͯ͠ऴྃ

Slide 17

Slide 17 text

Illuminate\Foundation\Exceptions\Handler::render() !17 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ͦΕҎ֎͸ྫ֎ΛΤϥʔϨεϙϯεʹม׵ ʢContet-Type ΍σόοάϞʔυͷ ON/OFF ౳ʹΑΔ੍ޚʣ

Slide 18

Slide 18 text

ΞδΣϯμ • ΍Γ͍ͨ͜ͱ • σϑΥϧτͷΤϥʔϋϯυϦϯάͷڍಈ • ࣮ݱख๏ !18

Slide 19

Slide 19 text

ಠࣗྫ֎ΛఆٛʢҰྫʣ !19

Slide 20

Slide 20 text

ಠࣗྫ֎ΛఆٛʢҰྫʣ !20

Slide 21

Slide 21 text

ಠࣗྫ֎ΛఆٛʢҰྫʣ !21

Slide 22

Slide 22 text

App\Exceptions\Handler ΛΧελϚΠζ • ࠓճ͸ report() ͸ͦͷ··Ͱ 🙆 • Τϥʔ࣌ͷ HTTP ϨεϙϯεΛ
 ಠࣗྫ֎ͷ getStatusCode() ͱ getUserMessage() Λ࢖ͬͯੜ੒͍ͨ͠ • → ͪΐͬͱָ͢Δํ๏Λ͝঺հ !22

Slide 23

Slide 23 text

Illuminate\Foundation\Exceptions\Handler::render() !23 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ࠶ܝ

Slide 24

Slide 24 text

Illuminate\Foundation\Exceptions\Handler::render() !24 /** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response */ public function render($request, Exception $e) { if (method_exists($e, 'render') && $response = $e->render($request)) { return Router::toResponse($request, $response); } elseif ($e instanceof Responsable) { return $e->toResponse($request); } $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $request->expectsJson() ? $this->prepareJsonResponse($request, $e) : $this->prepareResponse($request, $e); } ࠶ܝ

Slide 25

Slide 25 text

Illuminate\Foundation\Exceptions\Handler::prepareResponse() !25 /** * Prepare a response for the given exception. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Symfony\Component\HttpFoundation\Response */ protected function prepareResponse($request, Exception $e) { if (! $this->isHttpException($e) && config('app.debug')) { return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } if (! $this->isHttpException($e)) { $e = new HttpException(500, $e->getMessage()); } return $this->toIlluminateResponse( $this->renderHttpException($e), $e ); }

Slide 26

Slide 26 text

Illuminate\Foundation\Exceptions\Handler::prepareResponse() !26 /** * Prepare a response for the given exception. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Symfony\Component\HttpFoundation\Response */ protected function prepareResponse($request, Exception $e) { if (! $this->isHttpException($e) && config('app.debug')) { return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } if (! $this->isHttpException($e)) { $e = new HttpException(500, $e->getMessage()); } return $this->toIlluminateResponse( $this->renderHttpException($e), $e ); } Symfony\Component\HttpKernel\Exception\HttpException Λ
 Α͠ͳʹ HTTP Ϩεϙϯεʹม׵ͯ͘͠ΕΔ

Slide 27

Slide 27 text

ಠࣗྫ֎Λ HttpException ʹ
 ม׵Ͱ͖Δͱָ😄 !27

Slide 28

Slide 28 text

ಠࣗྫ֎Λ HttpException ʹม׵Ͱ͖Δͱָ😄 !28 getStatusCode(), $this->getUserMessage(), $this->getPrevious(), [], $this->code ); } } ௥Ճ ※આ໌͸ল͖·͕͢ JSON Ϩεϙϯεͷํ΋͏·͍͖͘·͢

Slide 29

Slide 29 text

App\Exceptions\Handler ΛΧελϚΠζ !29 /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { // 既存の render の仕組みを活用するため、HttpException に変換する if ($exception instanceof MyAppException) { $exception = $exception->toHttpException(); } return parent::render($request, $exception); } ௥Ճ

Slide 30

Slide 30 text

࢖͍ํ !30 getMessage()
 ͰHTTP Ϩεϙϯε༻ͷϝοηʔδʹ
 ΞΫηεՄೳ

Slide 31

Slide 31 text

·ͱΊ ಠࣗྫ֎ʹɺࣗ਎Λ HttpException ΁ม׵͢ΔϝιουΛͭ͘Δͱָ😄 !31

Slide 32

Slide 32 text

⾠஫ҙ • ʮྫ֎ΛͲ͏࢖͏͔ʯʹ͍ͭͯ͸͍Ζ͍Ζͳݟղ͕͋Δͱࢥ͍·͢ • Laravel Ͱ͸ 404 ౳ͷΫϥΠΞϯτΤϥʔʹ΋ྫ֎Λ࢖͍ͬͯΔͷͰ
 ͦΕʹͷͬͱͬͯྫ֎Λ࢖͏ͱ͍͏ํ๏Λ࠾༻͠·ͨ͠ !32

Slide 33

Slide 33 text

આ໌ͨ͠ιʔείʔυ +α ͸Լه URL ʹ΋ࡌ͍ͤͯ·͢ https://github.com/okashoi/colab-techbook6-example !33

Slide 34

Slide 34 text

!34

Slide 35

Slide 35 text

ʢ෇࿥ʣฤूͷաఔͰল͍ͨ
 εϥΠυ܊ !35

Slide 36

Slide 36 text

Illuminate\Foundation\Exceptions\Handler::report() !36 /** * Report or log an exception. * * @param \Exception $e * @return mixed * * @throws \Exception */ public function report(Exception $e) { if ($this->shouldntReport($e)) { return; } if (is_callable($reportCallable = [$e, 'report'])) { return $this->container->call($reportCallable); } try { $logger = $this->container->make(LoggerInterface::class); } catch (Exception $ex) { throw $e; } $logger->error( $e->getMessage(), array_merge($this->context(), ['exception' => $e] )); }

Slide 37

Slide 37 text

Illuminate\Foundation\Exceptions\Handler::report() !37 /** * Report or log an exception. * * @param \Exception $e * @return mixed * * @throws \Exception */ public function report(Exception $e) { if ($this->shouldntReport($e)) { return; } if (is_callable($reportCallable = [$e, 'report'])) { return $this->container->call($reportCallable); } try { $logger = $this->container->make(LoggerInterface::class); } catch (Exception $ex) { throw $e; } $logger->error( $e->getMessage(), array_merge($this->context(), ['exception' => $e] )); } ࢦఆ͞Εͨྫ֎ʹ͍ͭͯ͸Կ΋ͤͣʹऴྃ

Slide 38

Slide 38 text

Illuminate\Foundation\Exceptions\Handler::report() !38 /** * Report or log an exception. * * @param \Exception $e * @return mixed * * @throws \Exception */ public function report(Exception $e) { if ($this->shouldntReport($e)) { return; } if (is_callable($reportCallable = [$e, 'report'])) { return $this->container->call($reportCallable); } try { $logger = $this->container->make(LoggerInterface::class); } catch (Exception $ex) { throw $e; } $logger->error( $e->getMessage(), array_merge($this->context(), ['exception' => $e] )); } ྫ֎ࣗ৴͕ report() ϝιουΛ͍࣋ͬͯͨΒ ༏ઌతʹͦΕΛར༻

Slide 39

Slide 39 text

Illuminate\Foundation\Exceptions\Handler::report() !39 /** * Report or log an exception. * * @param \Exception $e * @return mixed * * @throws \Exception */ public function report(Exception $e) { if ($this->shouldntReport($e)) { return; } if (is_callable($reportCallable = [$e, 'report'])) { return $this->container->call($reportCallable); } try { $logger = $this->container->make(LoggerInterface::class); } catch (Exception $ex) { throw $e; } $logger->error( $e->getMessage(), array_merge($this->context(), ['exception' => $e] )); } ͦΕҎ֎͸ྫ֎ͷϝοηʔδͱ context Λϩάग़ྗ