简体   繁体   中英

Lumen 5.5 NotFoundHttpException handler

I'm running a small app on Lumen 5.5 and in my error handler, when I pass in a view as the content of my response, the headers get an error 500 instead of 404.

I've added a sample snippet, please consider I would only return one or the other response !

File : app/Exceptions/Handler.php

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
 public function render($request, Exception $e) {
    if($e instanceof NotFoundHttpException) {

        // This gives me a 404 in the browser dev console but not in the headers
        return response(view("errors.404"), 404);

        // This gives me a 404 in the headers
        return response('404 error', 404);

    }
    return parent::render($request, $e);
}

If I load this page in the browser DevTools, my GET has a status of 404, but if I scan with http checker tools online I get an error 500.

This is messing with my Adwords campaign so I had to switch for a plain response.

Since it's Lumen, I can't use the following that would work in Laravel :

return response(view('error.404', [], 404));

Thank you very much in advance.

How about this:

use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

if ($e instanceof NotFoundHttpException) {
    return new Response(
        view('errors.404'),
        $e->getStatusCode(),
        $e->getHeaders()
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM