Answer the question
In order to leave comments, you need to log in
How to correctly return a 404 code along with a view?
We have a controller and in it a method for the page.
In the absence of certain data, it is necessary to return a 404 error along with a pre-made view.
At the moment, the view just returns, without the 404 header.
The code:
if (is_null($page)) {
// Было так
return view('system.errors.404');
// Теперь так, однако хедеры не пишутся и возвращается 200 статус
return response()->view('system.errors.404', [], 404);
}
Answer the question
In order to leave comments, you need to log in
I don’t know what you can’t do there, but you don’t need to breed shit code with helpers and other heresy.
Everything is done in one line.
I didn’t find anything sensible in the manuals, so I wrote a small helper, an analogue of abort () for 404 status.
function abort404 ($code = 404, $message = '', array $headers = []) {
header(' ', true, $code);
return app()->abort($code, $message, $headers);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question