2016-11-10 10 views
0

コードに入る前に、私の目的を説明しましょう。私のWebアプリケーションは、販売する車両を表示します。私は、ユーザーが存在しないページにアクセスしようとすると、データベースに追加された最新の車12を表示するカスタム404ページが必要です。私は、次のしているLaravel 5.3でカスタム例外クラスとカスタムハンドラクラスを作成する

... CustomException.php \

のApp \例外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(); 
    } 
} 

のApp \例外はこれをテストするために、私は

を追加しました

throw new CustomException();

私のコントローラには、404Customビューが表示されません。これを実現するためには何が必要ですか?

UPDATE:モデルにクラスをバインドしている人のためのメモ。

app(MyClass :: class) - > functionNameGoesHere();を使用してクラス内の関数にアクセスしようとすると、BindingResolutionExceptionが発生します。

これを回避するには、サービスプロバイダのコンテナにクラスをバインドするのと同じ方法で変数を作成します。

私のコードは次のようになります。

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 –

関連する問題