symfony 的 require_once

2022-01-21 00:00:00 php symfony composer-php

我现在正在用 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:

  1. 将文件包含在 symfony 的 /vendors 目录下,或者
  2. 将每个类都包含为一项服务.

推荐答案

如果这些类位于 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

相关文章