简体   繁体   中英

How to install 3rd party library to Symfony 3.1 via composer?

I'm trying to develop a web application using Symfony 3.1 and I need to use some external library like Faker and Guzzle . However, everytime I try to install them with composer like this:

composer require fzaninotto/Faker

It always throws an errors like this:

Using version ^1.6 for fzaninotto/Faker
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing fzaninotto/faker (v1.6.0)
    Loading from cache

Writing lock file
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache


  [Symfony\Component\Config\Exception\FileLoaderLoadException]
  There is no extension able to load the configuration for "service" (in C:\xampp\htdocs\myapp\app/config\config.yml). Looked for namespace "service", found "framework", "securi
  ty", "twig", "monolog", "swiftmailer", "doctrine", "doctrine_migrations", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in C:\xampp\htdocs\myapp\app
  /config\config.yml (which is being imported from "C:\xampp\htdocs\myapp\app/config/config_dev.yml").



  [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
  There is no extension able to load the configuration for "service" (in C:\xampp\htdocs\myapp\app/config\config.yml). Looked for namespace "service", found "framework", "securi
  ty", "twig", "monolog", "swiftmailer", "doctrine", "doctrine_migrations", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution"


Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an exception

Installation failed, reverting ./composer.json to its original content.


  [RuntimeException]
  An error occurred when executing the ""cache:clear --no-warmup"" command:


    [Symfony\Component\Config\Exception\FileLoaderLoadException]

    There is no extension able to load the configuration for "service" (in C:\xampp\htdocs\myapp\app/config\config.yml). Looked for namespace "service", found "framework
  ", "securi
    ty", "twig", "monolog", "swiftmailer", "doctrine", "doctrine_migrations", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in C:\xampp\htdocs\res
  epkita\app
    /config\config.yml (which is being imported from "C:\xampp\htdocs\myapp\app/config/config_dev.yml").





    [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]

    There is no extension able to load the configuration for "service" (in C:\xampp\htdocs\myapp\app/config\config.yml). Looked for namespace "service", found "framework
  ", "securi
    ty", "twig", "monolog", "swiftmailer", "doctrine", "doctrine_migrations", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution"



  .


require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--] [<packages>]...

Do I really need to use a 3rd party symfony bundle for this or how can I use the library "as is"?

It seems you have put a "service" block inside your config_dev.yml and it means, there is no bundle installed and associated with that configuration.

Just remove that "service" block and you should be fine, because clean installation of symfony and running

composer require fzaninotto/Faker

works like a charm.

只需在config.yml文件中输入错误, config.yml编写service:而不是services: config.yml

You can autoload a third party library with composer like this

// composer.json
{
    // ...

    "autoload": {
        "psr-4": {
            "Custom\\Namespace\\": "relative/path/to/library"
        }
    }
}

The path must be relative to package root.

You can find more informations in the composer documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM