require_once at symfony(symfony 的 require_once)
问题描述
我现在正在用 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"这些不舒服的 require 语句,然后,如何将这些文件包含在 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:
- 将文件包含在 symfony 的
/vendors目录下,或者 - 将每个类都包含为一项服务.
推荐答案
如果这些类位于 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
这篇关于symfony 的 require_once的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:symfony 的 require_once
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
