-1

I'm trying to redirect any 404 errors from my image storage folder to an empty 404 error instead of the normal 404 template.

My images are through Glide and show up as: http://example.com/img/http/aHR0cHM6Ly93d3cudmFuY291dmVyY29udmVudGlvbmNlbnRyZS5jb20vaW1hZ2VzL2xvZ28tZGZkMDdhNzM5Y2NhNmQ2NDVkY2Y0NDg5OWIyNTQ5NWEuanBn?fm=webp&s=fe55826dcb196df2ae0f2e4055e9dab9

So they don't register as missing images, but pages not found.

So in my handler I'm running this to see if I get an response but its not doing anything yet:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use League\Glide\Filesystem\FileNotFoundException as GlideFileNotFoundException;

use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array<int, class-string<Throwable>>
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });



        $this->renderable(function (NotFoundHttpException $e, $request) {
            dump($request);
            if ($request->is('img/http/*')) {
                return response()->json([
                    'status' => 204,
                    'message' => 'Data not found'
                ], 200);


            }
        });
    }
}

But this seems to do nothing. Any ideas what I'm missing? I'm using the 204/200 error just to see anything change. The end goal is to just have a blank 404 server error for images.

Aaron
  • 237
  • 1
  • 7
  • Where is this code? Have you tried some basic debugging (e.g. `dump($request)` in the closure) to see if a) the function is being run and b) it's getting passed what you expect? – miken32 Aug 18 '22 at 18:19
  • @miken32 This code is in the handler.php file. And adding the dump like this shows nothing: ```$this->renderable(function (NotFoundHttpException $e, $request) { dump($request); });``` – Aaron Aug 19 '22 at 17:11
  • You said that already, but where is `handler.php`??? And do you mean `Handler.php`? Composer's autoloader is case-sensitive. – miken32 Aug 19 '22 at 18:50

1 Answers1

0

In the PHP file make sure you have at the top

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

ie:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
josezenem
  • 300
  • 1
  • 5
  • I do have that in the file already. Sorry for not showing the whole file. but is it updated on the main question now. – Aaron Aug 19 '22 at 17:11
  • @Aaron i tested by copying and pasting your file, then simply hitting somedomain.com/img/http/som.png everything seeemed to work correctly on my end, what kind of url example are you hitting? – josezenem Aug 19 '22 at 19:36