php 如何在 Symfony2 中获取根目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17274051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:36:25  来源:igfitidea点击:

How to get root dir in Symfony2

phpsymfony

提问by user2515999

I'm trying to get the root dir in symfony2.

我正在尝试获取 symfony2 中的根目录。

If I use:

如果我使用:

$this->get('kernel')->getRootDir();

I get this error:

我收到此错误:

FatalErrorException: Error: Call to undefined method Test\Component\ClassLoader\DebugClassLoader::get() 

How can I fix this?

我怎样才能解决这个问题?

回答by Jimbo

Edit, seeing as this post has garnered so much attention and mine is at the top, the best way to get the root directory is to pass it in to your class as a constructor argument. You would use services.ymlto do this, and in arguments:

编辑,看到这篇文章引起了如此多的关注,而我的在顶部,获取根目录的最佳方法是将其作为构造函数参数传递给您的类。你会services.yml用来这样做,并在参数中:

serviceName:
  class: Name\Of\Your\Service
  arguments: %kernel.root_dir%

Then, the following code will have the root directory given to it when the framework instantiates it:

然后,以下代码将在框架实例化它时为其提供根目录:

namespace Name\Of\Your;

class Service
{
    public function __construct($rootDir)
    {
        // $rootDir is the root directory passed in for you
    }
}

The rest of the answer below is the old, poor way of doing it without using Dependency Injection.

下面的其余答案是不使用依赖注入的旧的、糟糕的方法。



I want to make everyone aware that this is the Service Locator, which is an anti-pattern. Any developer should be able to see what a class, or controller, requires to function from the method signature only. Injecting a whole "container" is very generic, hard to debug and isn'tthe best way of doing things. You should use a Dependency Injection Containerthat allows you to inject specifically what you want anywhere in your application. Be specific. Check out a seriously awesomerecursively instantiating dependency injection container called Auryn. Where your framework resolves your controller / action, place it there and use the container to create the controller and run the method instead. Boom! Instant SOLID code.

我想让每个人都知道这是Service Locator,这是一种反模式。任何开发人员都应该能够仅从方法签名中看到类或控制器需要什么才能发挥作用。注入整个“容器”是非常通用的,难以调试,而且不是最好的做事方式。你应该使用依赖注入容器,它允许你在应用程序的任何地方注入你想要的东西。请明确点。查看一个非常棒的递归实例化依赖注入容器,称为Auryn. 在您的框架解析您的控制器/动作的地方,将其放置在那里并使用容器来创建控制器并运行该方法。繁荣!即时 SOLID 代码。

You're correct, the service container is accessed using $this->get('service').

你是对的,服务容器是使用$this->get('service').

However, in order to use $this->get(), you're going to need access to the get()method.

但是,为了使用$this->get(),您将需要访问该get()方法。

Controller Access

控制器访问

You gain access to this, and many other handy methods, by making sure your controller extends the base controller class that Symfony uses.

通过确保您的控制器扩展了 Symfony 使用的基本控制器类,您可以访问这个以及许多其他方便的方法。

Make sure you're referencing the correct Controller base class:

确保您引用了正确的 Controller 基类:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HelloController extends Controller
{
    /** The Kernel should now be accessible via the container **/
    $root = $this->get('kernel')->getRootDir();
}

Service Access

服务访问

If you want to access the container from a service, you're going to have to define your controller as a service. You can find more information in this post, this postand this postabout how to do this. Another useful link. Either way, you now know what to look for. This postmay also be useful:

如果要从服务访问容器,则必须将控制器定义为 service。你可以在这篇文章这篇文章这篇关于如何做到这一点的文章中找到更多信息。另一个有用的链接。无论哪种方式,您现在都知道要寻找什么。这篇文章也可能有用:

use Symfony\Component\DependencyInjection\ContainerInterface; 

class MyClass
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function doWhatever()
    {
        /** Your container is now in $this->container **/
        $root = $this->container->get('kernel')->getRootDir();
    }
}

In your config.yml, define your new type:

在 config.yml 中,定义新类型:

myclass:
  class: ...\MyClass
  arguments: ["@service_container"]

You can read more about the service container in the docs.

您可以在docs 中阅读有关服务容器的更多信息。

回答by Broncha

The parameter kernel.root_dirpoints to the appdirectory. Normally to get to the root directory, I user kernel.root_dir/../

该参数kernel.root_dir指向app目录。通常要进入根目录,我使用kernel.root_dir/../

So in controller you can use $this->container->getParameter('kernel.root_dir')."/../"

所以在控制器中你可以使用 $this->container->getParameter('kernel.root_dir')."/../"

In service definition you can use:

在服务定义中,您可以使用:

my_service:
    class: \Path\to\class
    arguments: [%kernel.root_dir%/../]

回答by Paul Andrieux

The best option is to declare tour class as a service in your services.yml file:

最好的选择是在 services.yml 文件中将旅游类声明为服务:

services:
    myclass:
        class: Your\Class\Namespace\MyClass
        arguments: ["@service_container"]

and adapt yhe constructor of you class:

并调整你的类的构造函数:

use Symfony\Component\DependencyInjection\ContainerInterface

class MyClass
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
}