Set 404 header if collection is empty

Hi,

I’ve got a custom route with a custom controller who fetch some pages to a new collection.
In my template, everything is working fine, I have a condition to detect if my collection is empty or not.
But I want to send a 404 header if collection is empty.
If I set this header in controller, it seems overriden because I have a 200 response.
Th only thing working for now is redirect to error page in controller, but it sends first a 302, and th URL changes.
Is it possible to send the 404 header from the controller, or from the template if collection is empty ?

Thank you

How are you setting the header?
Maybe this works:

$kirby->response()->code(404);
1 Like

It works, thank you so much.