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

[10.x] Wrap response preparation in events #47229

Merged
merged 4 commits into from May 30, 2023

Conversation

timacdonald
Copy link
Member

This enables user-land implementations of #45603 without actually bringing the feature to the framework.

For example:

public function boot(): void
{
    $logQueries = false;

    Event::listen(PreparingResponse::class, function () use (&$logQueries) {
        $logQueries = true;
    });

    Event::listen(ResponsePrepared::class, function () use (&$logQueries) {
        $logQueries = false;
    });

    DB::listen(function (QueryExecuted $event) use (&$logQueries) {
        if ($logQueries) {
            // log in production, throw locally.
        }
    });
}

@taylorotwell taylorotwell merged commit 59b822c into laravel:10.x May 30, 2023
31 checks passed
@timacdonald timacdonald deleted the response-hook branch June 1, 2023 03:29
@decadence
Copy link
Contributor

decadence commented Oct 20, 2023

@timacdonald PreparingResponse and ResponsePrepared are dispatched twice for simple view return. Is this by design or I missing something?

public function index()
{
    return view("index", [
        "title" => "Main Page"
    ]);
}

image

@decadence
Copy link
Contributor

Yes. Because prepareResponse runs twice (don't know why it's needed).

image

@timacdonald
Copy link
Member Author

The events will fire whenever a response is being "prepared". If the normal flow hits that method twice it will fire twice.

I also don't know why it does it twice, however at a quick glance it looks like it might be required so that responses returned via middleware are correctly "prepared".

@decadence
Copy link
Contributor

Ok, thanks. It runs for view preparation and before final response (after "after" middlewares).

duncanmcclean added a commit to statamic/cms that referenced this pull request Mar 25, 2024
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.

None yet

4 participants