2016-11-10 2 views
0

코드에 도달하기 전에 목표를 설명해 드리겠습니다. 내 웹 앱은 판매 할 차량을 표시합니다. 사용자가 존재하지 않는 페이지에 액세스하려고하면 최신 차량 12 개가 데이터베이스에 추가 된 맞춤 404 페이지가 필요합니다. 나는 다음과 같은 한Laravel 5.3에서 사용자 지정 예외 클래스 및 사용자 지정 처리기 클래스 만들기

... CustomException.php \

앱 \ 예외 CustomHandler.php

<?php 
namespace App\Exceptions; 

use Exception; 
use App\Exceptions\Handler as ExceptionHandler; 
use Illuminate\Contracts\Container\Container; 
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle; 
use Illuminate\Foundation\Exceptions\Handler; 
use Illuminate\Support\Facades\View; 

class CustomHandler extends ExceptionHandler 
{ 
    protected $vehicle; 

    public function __construct(Container $container, EloquentVehicle $vehicle) 
    { 
     parent::__construct($container); 

     $this->vehicle = $vehicle; 
    } 

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

    /** 
    * 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) 
    { 
     $exception = Handler::prepareException($exception); 

     if($exception instanceof CustomException) { 
      return $this->showCustomErrorPage(); 
     } 

     return parent::render($request, $exception); 
    } 

    public function showCustomErrorPage() 
    { 
     $recentlyAdded = $this->vehicle->fetchLatestVehicles(0, 12); 

     return View::make('errors.404Custom')->with('recentlyAdded', $recentlyAdded); 
    } 
} 

\

<?php 

namespace App\Exceptions; 

use Exception; 

class CustomException extends Exception 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
} 

앱 \ 예외는이를 테스트하려면 내가

추가

새로운 CustomException();을 던집니다.

내 컨트롤러에 있지만 404Custom보기를 표시하지 않습니다. 이 기능을 사용하려면 무엇을해야합니까?

업데이트 : 모델에 클래스를 바인딩 한 사람을위한 메모입니다. 클래스의 함수에 액세스하려고하면 BindingResolutionException이 발생합니다.

app (MyClass :: class) -> functionNameGoesHere();

이 문제를 해결하려면 서비스 제공 업체의 컨테이너에 클래스를 바인딩하는 것과 같은 방법으로 변수를 만듭니다.

내 코드는 다음과 같습니다

protected function showCustomErrorPage() 
{ 
    $eloquentVehicle = new EloquentVehicle(new Vehicle(), new Dealer()); 
    $recentlyAdded = $eloquentVehicle->fetchLatestVehicles(0, 12); 

    return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded); 
} 

미트의 버전

protected function showCustomErrorPage() 
{ 
    $recentlyAdded = app(EloquentVehicle::class)->fetchLatestVehicles(0, 12); 

    return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded); 
} 

답변

2

Laravel은 App\Exceptions\Handler 클래스의 render 함수를 호출합니다. 그래서 그것을 무시하는 것은 효과가 없을 것입니다.

App\Exceptions\Handler 클래스에만 추가해야합니다. 예를 들어

이의 돈에

<?php 

namespace App\Exceptions; 

use Exception; 
use Illuminate\Auth\AuthenticationException; 
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle; 
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 = [ 
     \Illuminate\Auth\AuthenticationException::class, 
     \Illuminate\Auth\Access\AuthorizationException::class, 
     \Symfony\Component\HttpKernel\Exception\HttpException::class, 
     \Illuminate\Database\Eloquent\ModelNotFoundException::class, 
     \Illuminate\Session\TokenMismatchException::class, 
     \Illuminate\Validation\ValidationException::class, 
    ]; 

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

    /** 
    * 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) 
    { 
     if($exception instanceof CustomException) { 
      return $this->showCustomErrorPage(); 
     } 

     return parent::render($request, $exception); 
    } 

    protected function showCustomErrorPage() 
    { 
     $recentlyAdded = app(EloquentVehicle::class)->fetchLatestVehicles(0, 12); 

     return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded); 
    } 
} 
+0

... 감사합니다. – VenomRush

+0

도와 드리겠습니다 : D –

관련 문제