Mohammad Fareed
Mohammad Fareed

Reputation: 1972

Access a Static File Located in Symfony Bundle

How can we access a html file located inside Bundle folder.?

Refer attached screen

  1. Possible with controller.?
  2. Possible with out controller.?

enter image description here

Upvotes: 0

Views: 378

Answers (1)

Goku
Goku

Reputation: 2139

Controller Access

You can access to the root dir thanks to this :

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

It will place into app/ directory and then you can navigate as you want

So in your case I think this will be work:

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

Service Access

You can access to the root dir by injecting container (dependency injection pattern)

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'

    }
}

In your services.yml, define your new service:

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

Upvotes: 2

Related Questions