skip to Main Content

I have just created a fresh Laravel project and installed Sentry for exception logging. Here is my bootstrap/app.php:

<?php

use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;
use IlluminateSupportFacadesAuth;
use SentryLaravelIntegration;
use SentryStateScope;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        Integration::handles($exceptions);
        Integration::configureScope(
            function (Scope $scope): void {
                $user = Auth::user();

                if ($user) {
                    $scope->setUser([
                        'id' => $user->id,
                        'email' => $user->email,
                        'username' => $user->name,
                    ]);
                }
            }
        );
    })->create();

When I try to cache my configurations by running:

php artisan config:cache

I get the following error:

IlluminateContractsContainerBindingResolutionException 

  Target class [hash] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:940
    936▕ 
    937▕         try {
    938▕             $reflector = new ReflectionClass($concrete);
    939▕         } catch (ReflectionException $e) {
  ➜ 940▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    941▕         }
    942▕ 
    943▕         // If the type is not instantiable, the developer is attempting to resolve
    944▕         // an abstract type such as an Interface or Abstract Class and there is

2

Answers


  1. Have a look at https://docs.sentry.io/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching.
    You can’t use closures in your config if you want to cache it.
    Also, you don’t need to set the user manually, we do this for you if you enable send_default_pii.

    Login or Signup to reply.
  2. bootstrap/app.php is the startup process for Laravel. You cannot use normal functions that are available after startup.

        ->withExceptions(function (Exceptions $exceptions) {
            Integration::handles($exceptions);
        })
        ->booted(function () {
            Integration::configureScope(
                function (Scope $scope): void {
                    $user = Auth::user();
    
                    if ($user) {
                        $scope->setUser([
                            'id' => $user->id,
                            'email' => $user->email,
                            'username' => $user->name,
                        ]);
                    }
                }
            );
        })->create();
    

    Alternatively, you can write this in AppServiceProvider@boot.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search