直接使用库而不是 Symfony2 中的包 [英] Working directly with libraries instead of bundles in Symfony2

查看:42
本文介绍了直接使用库而不是 Symfony2 中的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在现有的 Symfony2 项目中直接使用库.例如,我正在尝试添加 faker 库.我通过 composer 安装了它,但我不知道如何以及在哪里放置我需要的代码.

How can I use a library directly inside an existing Symfony2 project. I am, for instance, trying to add the faker library. I installed it via composer but I don't know how and where to put the code I need.

根据文档:

// require the Faker autoloader
require_once '/path/to/Faker/src/autoload.php';
// alternatively, use another PSR-0 compliant autoloader (like the Symfony2 ClassLoader for instance)

自动加载器的简单解释是什么?如何在没有捆绑的情况下直接使用库?库是否需要具有 autoload.php 文件以便它可以集成到 php 项目中?上面的代码放在哪里?

What is a simple explanation of auto loader? How to use a library directly without a bundle? Is it a requirement for a library to have an autoload.php file so that it can be integrated inside a php project? Where to put the above code?

有任何链接可以为新手解释这些概念吗?非常感谢您平时的指导.

Any links explaining such notions for newbies? Thank you very much for your usual guidance.

推荐答案

您无需进行任何配置.Faker 库是 PSR-4(参见 composer.json,第 23 行) 兼容所以只需安装它(通过 composer)并使用正确的命名空间.Symfony 自动加载 PSR-4/PSR-0 库/组件.像这样:

You do not need to config nothing. Faker library is PSR-4 (see composer.json, line 23) compliant so just install it (through composer) and use the proper namespace. Symfony automatically loads PSR-4 / PSR-0 libraries/components. Like this:

<?php # src/AppBundle/Controller/DefaultController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Faker;

class DefaultController extends Controller
{   
    public function indexAction()
    {

        $faker = Faker\Factory::create();
        var_dump($faker); die;
        // ...
    }
}

有用的链接:

这篇关于直接使用库而不是 Symfony2 中的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆