访问位于Symfony Bundle中的静态文件

时间:2016-08-03 07:07:41

标签: symfony

我们如何访问位于Bundle文件夹内的html文件。?

参见随附的屏幕

  1. 可以使用控制器。?
  2. 可能没有控制器。?
  3. enter image description here

1 个答案:

答案 0 :(得分:2)

控制器访问

由于这个原因,您可以访问根目录:

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

它将放入app/目录,然后您可以根据需要导航

所以在你的情况下,我认为这将是有效的:

$fileToYourPath = $this->get('kernel')->getRootDir().'/../src/C2Educate/ToolsBundle/Stripe/c2/c2.html'

服务访问

您可以通过注入容器(依赖注入模式)

来访问根目录
use Symfony\Component\DependencyInjection\ContainerInterface; 

class MyClass
{
    private $container;

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

    public function doWhatever()
    {
        $root = $this->container->get('kernel')->getRootDir();

        $fileToYourPath = $root.'/../src/C2Educate/ToolsBundle/Stripe/c2/c2.html'

    }
}

在您的services.yml中,定义您的新服务:

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