Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught ReflectionException: Class "config" does not exist #2097

Closed
adiafora opened this issue May 17, 2022 · 3 comments · Fixed by #2098
Closed

Fatal error: Uncaught ReflectionException: Class "config" does not exist #2097

adiafora opened this issue May 17, 2022 · 3 comments · Fixed by #2098

Comments

@adiafora
Copy link
Contributor

If I want to adhere to the principle of dependency injection, and add Spatie\Permission\Models\Role to the __construct of any class, I will get an error Fatal error: Uncaught ReflectionException: Class "config" does not exist

Because the helper config() is not initialized yet at the moment.

A simple example:

namespace App\Services\Permission;

class PermissionService
{
    public function __construct(
        private \Spatie\Permission\Models\Role $role,
    ) {
    }
}

Now in the terminal, try typing a command, for example php artisan cache:clear, and you will get an error.
I have a solution, I will offer it in PR ))

Versions
laravel/framework - v8.83.11
spatie/laravel-permission - 5.5.3
PHP 8.0.8
Windows 11

@systemsolutionweb
Copy link

I did try your example, and i didn't get any exception

php artisan cache:clear
Application cache cleared!

Maybe it is your implementation

@adiafora
Copy link
Contributor Author

@systemsolutionweb Yes, I looked more closely and found that only using dependency injection in the code does not give such an error. But if you use this PermissionService class in DI in the App\Console\Kernel constructor, then there will be an error with any access to the terminal.

namespace App\Console;

use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    public function __construct(
        Application $app,
        Dispatcher $events,
        private \App\Services\Permission $permission;
    }
}
namespace App\Services\Permission;

class PermissionService
{
    public function __construct(
        private \Spatie\Permission\Models\Role $role,
    ) {
    }
}

@spatie spatie locked and limited conversation to collaborators May 30, 2022
@freekmurze freekmurze converted this issue into discussion #2107 May 30, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants