route icon indicating copy to clipboard operation
route copied to clipboard

How can I assign controller to errors http ?

Open ericktucto opened this issue 1 year ago • 1 comments

I like send a Response if Method Not Found o route Not Found

example:

<?php
$router
  ->group("/dashboard", function () { /* code */ })
  ->set500([ErrorController::class, "handle500"])
  ->setStrategy(new ApplicationStrategy());
class ErrorController
{
  public function handle500($request)
  {
    return Response::html("<h1>Sorry, exists problem!</<h1>");
  }
  // handle401, handle401, handle402, handle403, handle404, handle419, handle429, handle503
}
$router
  ->group("/api/v1", function () { /* code */ })
  ->set500([ErrorJsonController::class, "handle500"])
  ->setStrategy(new JsonStrategy());
class ErrorJsonController
{
  public function handle500($request)
  {
    return Response::json(["message" => "Sorry, exists problem!"]);
  }
  // handle401, handle401, handle402, handle403, handle404, handle419, handle429, handle503
}

or

<?php
$router
  ->group("/dashboard", function () { /* code */ })
  ->setErrorHandle(ErrorController::class)
  ->setStrategy(new ApplicationStrategy());
class ErrorController
{
  // use dynamic
  public function handle500($request)
  {
    return Response::html("<h1>Sorry, exists problem!</<h1>");
  }
  // handle401, handle401, handle402, handle403, handle404, handle419, handle429, handle503
}
$router
  ->group("/api/v1", function () { /* code */ })
  ->setErrorHandle(ErrorJsonController::class)
  ->setStrategy(new JsonStrategy());
class ErrorJsonController
{
  // use dynamic
  public function handle500($request)
  {
    return Response::json(["message" => "Sorry, exists problem!"]);
  }
  // handle401, handle401, handle402, handle403, handle404, handle419, handle429, handle503
}

ericktucto avatar May 14 '23 20:05 ericktucto