php 如何从控制器内部获取到 Symfony2 中 web 目录的服务器路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9265788/
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-26 06:27:39  来源:igfitidea点击:

How to get the server path to the web directory in Symfony2 from inside the controller?

phpsymfony

提问by Sylvix

The question is as follows:

问题如下:

How can I get the server path to the web directory in Symfony2 from inside the controller (or from anywhere else for that reason)

如何从控制器内部(或出于这个原因从其他任何地方)获取到 Symfony2 中 Web 目录的服务器路径

What I've already found (also, by searching here):

我已经找到了什么(也可以在这里搜索):

This is advised in the cookbook article on Doctrine file handling

这是在关于 Doctrine 文件处理的食谱文章中建议的

$path = __DIR__ . '/../../../../web';

Found by searching around, only usable from inside the controller (or service with kernel injected):

通过搜索找到,只能从控制器内部使用(或注入内核的服务):

$path = $this->get('kernel')->getRootDir() . '/../web';

So, is there absolutely no way to get at least that 'web' part of the path? What if I, for example, decided to rename it or move or something?

那么,绝对没有办法至少获得路径的“网络”部分吗?例如,如果我决定重命名或移动或其他什么?

Everything was easy in the first symfony, when I could get like everything I needed from anywhere in the code by calling the static sfConfig::get() method..

在第一个 symfony 中,一切都很简单,当我可以通过调用静态 sfConfig::get() 方法从代码中的任何地方获得所需的一切时..

采纳答案by Martin

There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.

实际上没有直接的方法可以在 Symfony2 中获得 webdir 的路径,因为该框架完全独立于 webdir。

You can use getRootDir()on instance of kernel class, just as you write. If you consider renaming /webdir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see hereand here).

您可以getRootDir()在内核类的实例上使用,就像您编写的那样。如果您考虑/web将来重命名dir,则应使其可配置。例如 AsseticBundle 在其 DI 配置中有这样一个选项(见这里这里)。

回答by redjam13

To access the root directory from outside the controller you can simply inject %kernel.root_dir%as an argument in your services configuration.

要从控制器外部访问根目录,您只需%kernel.root_dir%在服务配置中作为参数注入即可。

service_name:
    class: Namespace\Bundle\etc
    arguments: ['%kernel.root_dir%']

Then you can get the web root in the class constructor:

然后你可以在类构造函数中获取 web 根:

public function __construct($rootDir)
{
    $this->webRoot = realpath($rootDir . '/../web');
}

回答by Molecular Man

You also can get it from any ContainerAware (f.i. Controller) class from the request service:

您还可以从请求服务的任何 ContainerAware (fi Controller) 类中获取它:

  • If you are using apache as a webserver (I suppose for other webservers the solution would be similar) and are using virtualhosting (your urls look like this - localhost/app.phpthen you can use:

    $container->get('request')->server->get('DOCUMENT_ROOT');
    // in controller:
    $this->getRequest()->server->get('DOCUMENT_ROOT');
    
  • Else (your urls look like this - localhost/path/to/Symfony/web/app.php:

    $container->get('request')->getBasePath();
    // in controller:
    $this->getRequest()->getBasePath();
    
  • 如果您使用 apache 作为网络服务器(我想其他网络服务器的解决方案是类似的)并且正在使用虚拟主机(您的网址如下所示 -localhost/app.php那么您可以使用:

    $container->get('request')->server->get('DOCUMENT_ROOT');
    // in controller:
    $this->getRequest()->server->get('DOCUMENT_ROOT');
    
  • 否则(您的网址如下所示 - localhost/path/to/Symfony/web/app.php

    $container->get('request')->getBasePath();
    // in controller:
    $this->getRequest()->getBasePath();
    

回答by Thomas Decaux

You are on Symfony, think "Dependency Injection" ^^

你在 Symfony 上,想想“依赖注入”^^

In all my SF project, I do in parameters.yml:

在我所有的 SF 项目中,我在 parameters.yml 中做:

web_dir: "%kernel.root_dir%/../web"

So I can safely use this parameter within controller:

所以我可以安全地在控制器中使用这个参数:

$this->getParameter('web_dir');

回答by noel

My solution is to add this code to the app.php

我的解决方案是将此代码添加到 app.php

define('WEB_DIRECTORY', __DIR__);

The problem is that in command line code that uses the constant will break. You can also add the constant to app/console file and the other environment front controllers

问题是在命令行中使用常量的代码会中断。您还可以将常量添加到 app/console 文件和其他环境前端控制器

Another solution may be add an static method at AppKernel that returns DIR.'/../web/' So you can access everywhere

另一种解决方案可能是在 AppKernel 添加一个返回DIR.'/../web/'的静态方法,这样你就可以在任何地方访问

回答by Tek

UPDATE: Since 2.8 this no longer works because assetic is no longer included by default. Although if you're using assetic this will work.

更新:从 2.8 开始,这不再有效,因为默认情况下不再包含资产。尽管如果您使用的是资产,这将起作用。

You can use the variable %assetic.write_to%.

您可以使用变量 %assetic.write_to%.

$this->getParameter('assetic.write_to');

$this->getParameter('assetic.write_to');

Since your assets depend on this variable to be dumped to your web directory, it's safe to assume and use to locate your web folder.

由于您的资产依赖于这个变量被转储到您的 web 目录,因此可以安全地假设并使用它来定位您的 web 文件夹。

http://symfony.com/doc/current/reference/configuration/assetic.html

http://symfony.com/doc/current/reference/configuration/assetic.html

回答by utkarsh2k2

For Symfony3 In your controller try

对于 Symfony3 在您的控制器中尝试

$request->server->get('DOCUMENT_ROOT').$request->getBasePath()

回答by Ashok

$host = $request->server->get('HTTP_HOST');
$base = (!empty($request->server->get('BASE'))) ? $request->server->get('BASE') : '';
$getBaseUrl = $host.$base;

回答by Tsounabe

Since Symfony 3.3,

从 Symfony 3.3 开始,

You can use %kernel.project_dir%/web/instead of %kernel.root_dir%/../web/

您可以使用%kernel.project_dir%/web/代替%kernel.root_dir%/../web/