코드에 도달하기 전에 목표를 설명해 드리겠습니다. 내 웹 앱은 판매 할 차량을 표시합니다. 사용자가 존재하지 않는 페이지에 액세스하려고하면 최신 차량 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);
}
... 감사합니다. – VenomRush
도와 드리겠습니다 : D –