在 Symfony2 的供应商文件夹中添加 Bundle 时出错

Getting error in add Bundle in Vendor Folder in Symfony2

Bundle Name : 打开tok(我从github下载的)

我将该文件夹放在 Symfony 的供应商文件夹中(项目 name\vendor\OpenTok\OpenTok\and 所有文件和文件夹都在这里)

AppKernel.php

<?php

 use Symfony\Component\HttpKernel\Kernel;
 use Symfony\Component\Config\Loader\LoaderInterface;

 class AppKernel extends Kernel
 {
     public function registerBundles()
     {
        $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new OpenTokBundle\OpenTokBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AdminBundle\AdminBundle(),
        new SiteBundle\SiteBundle(),
        new WSBundle\WSBundle(),
    );

    if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }

    return $bundles;
   }

  public function registerContainerConfiguration(LoaderInterface $loader)
  {
      $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
   }
}

app\autoload.php

 <?php

  use Doctrine\Common\Annotations\AnnotationRegistry;
  use Composer\Autoload\ClassLoader;
  /**
  * @var ClassLoader $loader
  */
  $loader = require __DIR__.'/../vendor/autoload.php';
  AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
  $loader->add('OpenTok' , __DIR__.'/..vendor/OpenTok');
  return $loader;

 ?>

Composer.json

  "require": {
        "php": ">=5.3.9",
        "symfony/symfony": "2.8.*",
        "doctrine/orm": "^2.4.8",
        "doctrine/doctrine-bundle": "~1.4",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "~2.0", 
       "opentok/opentok": "dev-master"
   },

上面的代码给我错误:

Fatal error: Class 'OpenTokBundle\OpenTokBundle' not found

现在我如何在所有其他文件中使用 opentok Bundle?请帮助我。我是 symfony 新手

您的问题是由斜杠引起的。
它应该是 $loader->add('OpenTok' => __DIR__.'/../vendor/OpenTok'); 和另一行一样(虽然我不确定实际路径是否正确)。

话虽如此,您实际上应该使用 composer 下载和管理包,因为它具有仅下载存储库不会包含的依赖项。

为此:

  1. 从自动加载中删除 $loader->add('OpenTok' => __DIR__.'/..vendor/OpenTok');
  2. "opentok/opentok": "^2.3.2" 或您要下载的版本添加到 composer.json
  3. 中的 "require" 密钥
  4. 运行 composer update opentok/opentok.

包和 类 现在应该在您的应用程序中可用,当您需要更新版本时,您可以通过 composer 管理它,而不需要手动下载包(以及所有依赖项) ).

我认为你应该使用 composer 管理所有,你需要安装库 opentok/opentok 和 symfony 的包 joos/open-tok-bundle 所以在你的 composer.json...

"require": {
    "php": ">=5.3.9",
    "symfony/symfony": "2.8.*",
    "doctrine/orm": "^2.4.8",
    "doctrine/doctrine-bundle": "~1.4",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "~2.0",
    "joos/open-tok-bundle": "2.3.x-dev", 
    "opentok/opentok": "dev-master"
   },

运行 作曲家安装

在你的AppKernel.php

<?php

 use Symfony\Component\HttpKernel\Kernel;
 use Symfony\Component\Config\Loader\LoaderInterface;

 class AppKernel extends Kernel
 {
     public function registerBundles()
     {
        $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Joos\OpenTokBundle\JoosOpenTokBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AdminBundle\AdminBundle(),
        new SiteBundle\SiteBundle(),
        new WSBundle\WSBundle(),
    );

    if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }

    return $bundles;
   }

  public function registerContainerConfiguration(LoaderInterface $loader)
  {
      $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
   }
}

由此urlhttps://github.com/djoos/JoosOpenTokBundle