"Laravel 라우팅 예외처리"의 두 판 사이의 차이

(새 문서: ==개념== * 라라벨에서 routes.php 파일에 정의하지 않은 경로를시도 할 경우 사용자가 지정한 라우트로 리다이렉트 되도록 하는 방법 <source l...)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개념==
==개념==
* 라라벨에서 routes.php 파일에 정의하지 않은 경로를시도 할 경우 사용자가 지정한 라우트로 리다이렉트 되도록 하는 방법
* 라라벨에서 routes.php 파일에 정의하지 않은 경로를 접근할 경우 지정된 곳으로 리다이렉트되게 하는 방법
 
;→ <code>app\Exceptions\Handler.php</code> 파일의 Handler class의 render 메소드에 조건을 추가
<source lang="php">
<syntaxhighlight lang="php">
<?php
<?php


54번째 줄: 54번째 줄:
             switch ($e->getStatusCode())  
             switch ($e->getStatusCode())  
             {
             {
                 // not found
                 // 404 not found
                 case 404:
                 case 404:
                     return redirect()->guest('post');
                     return redirect()->guest('post');
                     break;
                     break;


                 // internal error
                 // 500 internal error
                 case '500':
                 case '500':
                     return redirect()->guest('post');
                     return redirect()->guest('post');
75번째 줄: 75번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==같이 보기==
* [[Laravel 라우팅]]


[[분류: laravel]]
[[분류: laravel]]

2020년 11월 2일 (월) 02:57 기준 최신판

1 개념[ | ]

  • 라라벨에서 routes.php 파일에 정의하지 않은 경로를 접근할 경우 지정된 곳으로 리다이렉트되게 하는 방법
app\Exceptions\Handler.php 파일의 Handler class의 render 메소드에 조건을 추가
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if($this->isHttpException($e))
        {
            switch ($e->getStatusCode()) 
            {
                // 404 not found
                case 404:
                    return redirect()->guest('post');
                    break;

                // 500 internal error
                case '500':
                    return redirect()->guest('post');
                    break;

                default:
                    return $this->renderHttpException($e);
                    break;
            }
        }
        else
        {        
            return parent::render($request, $e);
        }
    }
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}