0

I'm googling and googling and can't find what I need.

How to get path to "web" directory in symfony2 in

  • action [no idea, i only found solution for older versions of symfony:
    $sf_relative_url_root = sfContext::getInstance()->getRequest()->getRelativeUrlRoot();
    I've tried also $this->get('security.context')->getRequest()->getRelativeUrlRoot(), but it doesn't work]

  • .js file

In case of .js file I do for now in twig template something like that:

<script type="text/javascript">
var asset_url="{{asset(' ')}}"; 
asset_url = asset_url.substr(0, asset_url.length-1);
</script>
<script href="{{asset('js/script.js')}}" type="text/javascript"></script>

is a better way?

4
  • I don't get it, why do you need first javascript code? why not just use {{ asset('js/script.js') }} ? it should work just fine.. Commented Jan 5, 2012 at 13:10
  • Are you sure this is a question on Symfony2? Commented Jan 5, 2012 at 14:23
  • First js code is written, because i can't use {{asset('smth')}} in .js file and i ask how to do that. @elnur: in case of first point i think so. Commented Jan 5, 2012 at 14:42
  • For now I use in script.js variable asset_url to do things connected with path to web directory. Commented Jan 5, 2012 at 14:50

1 Answer 1

1

If I understood your question correctly, then what you're looking for is $this->getRequest()->getUri() in a controller class.

Take a look at Symfony\Component\HttpFoundation\Request class if it's not, you will most likely find it there.

Sign up to request clarification or add additional context in comments.

4 Comments

almost, it returns: localhost/project/main/app_dev.php/action I would like only localhost/project/main but it's easy to cut:) Second question left[js file] - is there better way?
Maybe Request::getBasePath() then? as for .js easiest way I can think of is to set base_url param in your parameters.ini, then you can access it at any point as twig variable. It will also work for controller
Thanks! Actually it should be $this->getRequest()->getBasePath(). In case of js file i can't use twig things in .js file, if i could i would use {{asset('smth')}}
To get full URL I do for example: $url = 'http://'.$this->getRequest()->headers->get('host').$this->getRequest()->getBasePath().'/scripts/script.php'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.