D
D
Dmitry2016-01-22 14:41:57
Laravel
Dmitry, 2016-01-22 14:41:57

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

2 answer(s)
A
Andrzej Wielski, 2016-01-22
@another_dream

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.

D
Dmitry, 2016-01-22
@another_dream

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

If anyone else has ideas - unsubscribe, perhaps there is a more correct approach to the task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question