gizmojo's avatar

Livewire change response status code in full page component

Is it possible to set the response status code? E.g I have a full-page livewire component that servers a pretty 404 but need to set the status code to 404.

Standard controller I do it by:

return response(view('products.not_available'), 404);
0 likes
4 replies
MohamedTammam's avatar

When talking about the HTTP statuses. It's about the whole request. Livewire serves part of the page, not the whole page, not the whole response.

If you want to provide 404 code, then do that in the controller itself.

1 like
gizmojo's avatar

@MohamedTammam thanks - I would need to use a standard controller with a view that renders a "full page" livewire component?

MohamedTammam's avatar
Level 51

@gizmojo If you want the server to send 404 code for the response, then yes.

The way I do it is by using a controller to load data and then pass this data to the livewire component. But solution might not be good for all cases.

1 like
Muetze's avatar

I know this topic is old and probably no longer of interest to the author. But it was the first result I found when I searched the web. So I’m replying anyway.

Tested with LW 4:

    public function render()
    {
        return view('my-template')
            ->response(fn (Response $response) => $response->setStatusCode(403));
    }

Please or to participate in this conversation.