php - 如何在 Symfony2 中获取根目录

标签 php symfony

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

如果我使用:

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

我收到这个错误:

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

我该如何解决这个问题?

最佳答案

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.yml to do this, and in arguments:

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

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

namespace Name\Of\Your;

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

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


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't the best way of doing things. You should use a Dependency Injection Container that allows you to inject specifically what you want anywhere in your application. Be specific. Check out a seriously awesome recursively 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.

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

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

Controller 访问

通过确保您的 Controller 扩展 Symfony 使用的基 Controller 类,您可以访问此方法和许多其他方便的方法。

确保您引用了正确的 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();
}

服务访问

如果你想从服务访问容器,你将不得不 define your controller as a service .您可以在 this post 中找到更多信息, this postthis post关于如何做到这一点。另一个useful link .不管怎样,你现在知道要寻找什么了。 This post也可能有用:

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();
    }
}

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

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

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

关于php - 如何在 Symfony2 中获取根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17274051/

相关文章:

php - 为什么 "104"排在 "0000105"之前?

php - SilverStripe CropperField 选项

symfony - Doctrine2/Symfony2-同一表上有多个实体

php - 在 Symfony 中重定向回两步

php - Symfony2 使用 Multiuser Bundle 和 Alice Bundle

php - strtotime 使用日期输入

php - 通过 touch() 更改文件的最后修改时间并通过 filemtime() 获取结果

php - Symfony YAML 用逗号解析内容

php - 带有表单选项问题的 Fine Uploader

php - symfony 3 的缓存失效是如何工作的?