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

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from

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());
    }
};

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.

1 participant