Use Symfony 2 component in non-Symfony project?

我与影子孤独终老i 提交于 2019-12-05 12:38:41

Manage your dependencies with composer.

First create a composer.json file in your project folder :

{
    "require": {
        "symfony/translation": "2.4.*"
    }
}

Then download composer and run it :

wget http://getcomposer.org/composer.phar
php composer.phar install

You can now use your component by importing the composer autoloader :

<?php

    require_once('vendor/autoload.php');

    use Symfony\Component\Translation\Translator;
    use Symfony\Component\Translation\MessageSelector;
    use Symfony\Component\Translation\Loader\ArrayLoader;

    $translator = new Translator('fr_FR', new MessageSelector());
    $translator->setFallbackLocales(array('fr'));
    $translator->addLoader('array', new ArrayLoader());
    $translator->addResource('array', array(
        'Hello World!' => 'Bonjour',
    ), 'fr');

    echo $translator->trans('Hello World!')."\n";

What about using Composer to manage your dependencies.

The point here is that it also manages autoloading,

From the documentation,

Autoloading#

Besides downloading the library, Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process:

require 'vendor/autoload.php';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!