Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I assign controller to errors http ? #331

Open
ericktucto opened this issue May 14, 2023 · 1 comment
Open

How can I assign controller to errors http ? #331

ericktucto opened this issue May 14, 2023 · 1 comment

Comments

@ericktucto
Copy link

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
}
@bertramakers
Copy link

You need to implement a custom strategy (you can extend the existing ApplicationStrategy or JsonStrategy), and implement or override the getThrowableHandler() method in your custom strategy.

This method should return an implementation of MiddlewareInterface that forwards the given ServerRequestInterface to the next RequestHandlerInterface, wrapped it in a try/catch so that it can catch any Throwables and convert them to responses.

Take a look at the implementation in JsonStrategy for an example: https://github.com/thephpleague/route/blob/5.x/src/Strategy/JsonStrategy.php#L64-L98

You can then use your custom strategy on a route group, a single route, or the whole router depending on your needs. In your case I think you'll need to create 2 custom strategies, one for HTML pages and one for JSON endpoints, and assign them to the right routes/groups like you did with the default ApplicationStrategy and JsonStrategy in your example.

Disclaimer: I'm not the author/maintainer of this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants