Laravel 5.2.x test if provider exists

What is the best way to check if a provider exists in our Laravel project?

My decision:

$foo = 'Foo\Foo';

if (class_exists($foo)) {  
    // class exists
}

Is there a better solution? Laravel method I don't know?

Thank!

+4
source share
3 answers

There is no function in Laravel to make the right path.

But the most correct way would be to verify the existence of the class using the full namespace, for example:

$foo = 'Symfony\Component\HttpFoundation\Request';
if (class_exists($foo)) {
    // class exists
}
+1
source

I really do not think that there really is a better way, that is, to say that there is no good API for it that I know of.

, , Laravel, Composer installed.json ( , vendor/composer/installed.json), , , .

Laravel, , Laravel - IoC, - config ('app.providers'), , , , , app('the.binding.name') , ( $app->bound('the.binding.name'), ).

, , , , , , . , , , , .

0

I think you should try this with larvel using the application container as follows:

try {
    app($foo);
} catch (\ReflectionException $e) {
    // here you know that class dosen't exists
}
0
source

All Articles