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

[5.x] Allow custom render callback for overridden exceptions #11408

Merged

Conversation

FrittenKeeZ
Copy link
Contributor

Upgrading from Statamic 4 and Laravel 10 to Statamic 5 and Laravel 11 I wanted to migrate this custom exception handler:

public function render($request, \Throwable $e)
{
    if ($e instanceof NotFoundHttpException) {
        $eventsMount = Collection::findByHandle('events')->mount();
        if (Str::before($request->path(), '/') === $eventsMount->slug()) {
            return response()->redirectTo($eventsMount->url());
        }
    }

    return parent::render($request, $e);
}

Following Laravel's documentation I couldn't understand why it didn't work, until I dived into the exception and saw it implements a render method which takes over.

This PR introduces a static callback you can use to implement your own handling:

NotFoundHttpException::$renderCallback = function (Request $request) {
    $eventsMount = Collection::findByHandle('events')->mount();
    if (Str::before($request->path(), '/') === $eventsMount->slug()) {
        return response()->redirectTo($eventsMount->url());
    }
};

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I made a couple of tweaks.

  • Changed to a renderUsing method which matches existing Laravel/Statamic conventions.
  • The callback is moved to the start of render, so you can technically override the whole thing. You'll need to handle API situations though.
  • The closure's $this will be the exception.
  • If you don't return anything, the default behavior continues. (Yours was doing this already though).
NotFoundHttpException::renderUsing(function (Request $request) {
    //
});

Yours would look something like this:

NotFoundHttpException::renderUsing(function (Request $request) {
    if (Statamic::isCpRoute() || Statamic::isApiRoute() || $this->getStatusCode() !== 404) {
        return;
    }

    $eventsMount = Collection::findByHandle('events')->mount();
    if (Str::before($request->path(), '/') === $eventsMount->slug()) {
        return response()->redirectTo($eventsMount->url());
    }
};

@jasonvarga jasonvarga merged commit 970ee2b into statamic:5.x Feb 5, 2025
18 checks passed
@FrittenKeeZ FrittenKeeZ deleted the feature/exceptions-render-callback branch February 6, 2025 08:19
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

Successfully merging this pull request may close these issues.

2 participants