Creating a custom exception class and custom handler class in Laravel 5.3 -
before code, let me explain aim. web app displays vehicles sale. have need custom 404 page display 12 of latest vehicles added database if user tries access page doesn't exist.
i have following...
app\exceptions\customexception.php
<?php namespace app\exceptions; use exception; class customexception extends exception { public function __construct() { parent::__construct(); } }
app\exceptions\customhandler.php
<?php namespace app\exceptions; use exception; use app\exceptions\handler 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 exception. * * great spot send exceptions sentry, bugsnag, etc. * * @param \exception $exception * @return void */ public function report(exception $exception) { parent::report($exception); } /** * render exception 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); } }
to test added
throw new customexception();
to controller doesn't bring 404custom view. need working?
update: note who's bound class model. you'll bindingresolutionexception if try access function in class using:
app(myclass::class)->functionnamegoeshere();
to around create variable in same way bind class container in service provider.
my code looks this:
protected function showcustomerrorpage() { $eloquentvehicle = new eloquentvehicle(new vehicle(), new dealer()); $recentlyadded = $eloquentvehicle->fetchlatestvehicles(0, 12); return view()->make('errors.404custom')->with('recentlyadded', $recentlyadded); }
amit's version
protected function showcustomerrorpage() { $recentlyadded = app(eloquentvehicle::class)->fetchlatestvehicles(0, 12); return view()->make('errors.404custom')->with('recentlyadded', $recentlyadded); }
laravel calls render
function of app\exceptions\handler
class. overriding not work.
you have add in app\exceptions\handler
class only.
for example:
<?php namespace app\exceptions; use exception; use illuminate\auth\authenticationexception; use app\project\frontend\repo\vehicle\eloquentvehicle; use illuminate\foundation\exceptions\handler exceptionhandler; class handler extends exceptionhandler { /** * list of exception types should not 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 exception. * * great spot send exceptions sentry, bugsnag, etc. * * @param \exception $exception * @return void */ public function report(exception $exception) { parent::report($exception); } /** * render exception 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); } }
Comments
Post a Comment