require_once在symfony [英] require_once at symfony
问题描述
我现在正在使用php + Symfony2框架,我有以下代码:
I'm making now things with php + Symfony2 framework, and I have the following code:
require_once("one_file.php");
require_once("another_file.php");
...等等。
问题是,如何Symfonyze这些不舒服的要求句子,以后,如何包括这些文件在Symfony2包中?
The problem is, how to "Symfonyze" these uncomfortable require sentences, and after, how to include these files in the Symfony2 package?
我们已经考虑了两种可能性:
We've thought about two possibilities:
- 将文件包含在
/ vendors
symfony目录或 - 包含每个类作为服务。
- Include the file at
/vendors
directory of symfony, or - Include each class as a service.
推荐答案
类驻留在bundle中,然后你可以使用如下:
假设你的bundle名称是AcmeDemoBundle。将此文件放在Acme / DemoBundle / Model /
If these classes reside inside bundle then you could use as below: Suppose your bundle name is AcmeDemoBundle. Place this file inside Acme/DemoBundle/Model/
//one_file.php
namespace Acme/DemoBundle/Model;
class one_file {
...........
}
要在控制器中使用此文件或任何其他文件:
To use this file inside your controller or any other file:
此处适用于Acme / DemoBundle / Controller / UserController.php
Here for Acme/DemoBundle/Controller/UserController.php
namespace Acme/DemoBundle/Controller
use Acme/DemoBundle/Model/one_file
class UserController {
public $one_file=new one_file();
}
在php 5.3中,引入了命名空间。你应该看看命名空间及其在php文档中的用途
In php 5.3 onwards, namespaces has been introduced. You should probably look at namespaces and its uses in php documentation
这篇关于require_once在symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!