使用 Symfony2 包含 3rd 方文件的最佳方法 [英] Best approach To include 3rd party files with Symfony2

查看:33
本文介绍了使用 Symfony2 包含 3rd 方文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 symfony2 中包含 3rd 方 php 文件的最佳方法是什么.我使用不同的 php - ajax 包在我的 symfony2 应用程序中上传文件.该软件包为我提供了一些需要在 symfony 控制器中使用的 php oops 代码.我正在我的控制器中创建该代码的对象.所以我想知道我可以在哪里放置第三方代码或文件,以及如何在我的 symfony2 控制器中包含或创建该代码的对象.我们是否也在 symfony2 中使用 require 或 include.如果是,那是唯一的方法.

I would like to know What is the best way to include 3rd party php files in symfony2. I am using a different php - ajax package for uploading files in my symfony2 application. The package offers me some php oops code which i need to use in my symfony controller. I am creating objects of that code in my controller. So i would like to know where i can put that third party code or file and how can i include or create objects of that code in my symfony2 controller. Do we use require or include in symfony2 as well. If So is that the only approach.

推荐答案

我不太确定是否尝试将命名空间添加到第三方库.例如,Twig 不使用名称空间.而且真的没有必要.例如,考虑您想要使用 Zend_Framework 1 库中的 PDF 组件的情况.

I'm not so sure about trying to add namespaces to a third party library. Twig, for example, does not use name spaces. And there really is no need. Consider for example a case where you want to use the PDF component from the Zend_Framework 1 library.

在您的 app/autoload.php 文件中,您将执行以下操作:

In your app/autoload.php file you would do something like:

$loader->registerPrefixes(array(
    'Twig_Extensions_' => $ws . 'Symfony/vendor/twig-extensions/lib',
    'Twig_'            => $ws . 'Symfony/vendor/twig/lib',
    'Zend_'            => $ws . 'ZendFramework-1.0.0/library',
));

// And since Zend internally uses require/include we need to set an include path
ini_set('include_path','.' .

    PATH_SEPARATOR . $ws . 'ZendFramework-1.0.0/library'

);

此时我们应该能够在控制器内部创建第三部分对象,同时让自动加载系统负责查找和包含类:

At this point we should be able to create 3rd part objects inside of controllers while letting the autoload system take care of finding and including the classes:

    $page = new \Zend_Pdf_Page(\Zend_Pdf_Page::SIZE_A4);
    $doc->pages[] = $page;

    $font1 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
    $font2 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_COURIER_BOLD);

您确实必须使用 \ 来解决缺少命名空间的问题.

You do have to use the \ to get around the lack of namespacing.

此答案确实假定您的第 3 部分库遵循或多或少的标准类命名约定.如果它有自己的自动加载功能,那么也只需从 autolaod.php 调用它.如果您根本不想使用自动加载,那么只需设置包含路径并将其包含在内即可.

This answer does assume that your 3rd part library follows the more or less standard class naming convention. If it has it's own auto loading functionality then just call it from autolaod.php as well. And if you don't want to use autoloading at all then just set the include path and include away.

这篇关于使用 Symfony2 包含 3rd 方文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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