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
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
.bootstrap/app.php
is the startup process for Laravel. You cannot use normal functions that are available after startup.Alternatively, you can write this in AppServiceProvider@boot.