1

I am new in symfony2. i am stuck at small problem. my directory path is like this

project

/app
/bin
/src
  /neil
    /mybundle
       /Controller  
       /DependencyInjection
       /EventListner
       /Helper
       /Resources
         /public
           /js
            /script.js
       /Services
       /Test
/vendor
/web

I want to access the url of script.js in my controller/services(not in template). How would i do it.

edit: I have tried

$fileLocator = $this->container->get('file_locator');
        $path = $fileLocator->locate('@neilmybundle/Resources/public/js/script.js');

It returns directory path. If i use this directory path like

$r = '<script type="text/javascript" src="'.$path.'"></script>';

        return new Response($r);

It does not source the file.

NeiL
  • 791
  • 8
  • 35

2 Answers2

1

In your controller/service, you can inject the kernel (or grab it from the container) and use locateResource(). See: https://stackoverflow.com/a/7587106/4000591

Community
  • 1
  • 1
0

This will help you: https://stackoverflow.com/a/16498845/1353837

I think you should execute app/console assets:install before

Community
  • 1
  • 1
Sergei Gorjunov
  • 1,789
  • 1
  • 14
  • 22