开发者

Dependencies Symfony2

I'm wondering if there is a proper way to check the dependencies.

For example I've got a NewsBundle. Now 开发者_JS百科I'll have to check if there is a CommentBundle. If there is one, it should execute a few more Code.

Any suggestions?


In addition to markymark's answer, you can check if a specific service exists from your controller (or any other container-aware code) with the following snippet:

if ($this->container->has('foo_service.alias'))
{
    // service is loaded and usable
}

If you're not sure of the exact alias of a given service, or just for kicks and giggles, you can run the console command php app/console container:debug to see all services registered with the container.


You could use class_exists on the main Bundle class that every bundle should have.

For example:

if (class_exists('Acme\CommentBundle\AcmeCommentBundle'))
{
    // Bundle exists and is loaded by AppKernel...
}


The Kernel class contains a list of helper methods to check if a certain class is part of an active bundle or if a bundle is registered.

public BundleInterface[] getBundles()
    Gets the registered bundle instances.

public bool isClassInActiveBundle(string $class)
    Checks if a given class name belongs to an active bundle.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜