Asked  7 Months ago    Answers:  5   Viewed   30 times

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)

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

This is advised in the cookbook article on Doctrine file handling

$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..

 Answers

85

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

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

Wednesday, March 31, 2021
 
RompelStompel
answered 7 Months ago
65

You need to put any strings starting with @ in quotes:

arguments:
    ["@security.context", "@fos_user.user_manager"]

Reserved characters, like @, should be quoted. Unquoted @s were deprecated in Symfony 2.8. Symfony 3.0 will forbid you to use such definitions and will throw an exception.

If you don't have control over the bundle that registered the problematic configuration, send a pull request with a fix to the vendor. As a quick fix you can downgrade Symfony. 2.7 will work, as well as 2.8 (the later will only emit a deprecation notice).

Saturday, May 29, 2021
 
FWH
answered 5 Months ago
FWH
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

<phpunit>
    <!-- ... -->
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
 
Bálint Molnár
answered 3 Months ago
51

Use the Application.StartupPath property, it always points to directory where your EXE is located:

  using System.IO;
  ...

     string exeFolder = Application.StartupPath;
     string reportPath = Path.Combine(exeFolder, @"Reportsreport.rdlc");

You'll want to make sure the report gets copied to your binDebugReports folder as well so it will work in the IDE. Use xcopy /s /d in a post-build event to get the file(s) copied.

Thursday, August 12, 2021
 
PLPeeters
answered 3 Months ago
46

Simply opening and reading directories does not change the current working directory. However, changing directory in your program will.

for reference,

#include <unistd.h>
#include <stdio.h>

int main() {
    char cwd[1024];
    chdir("/path/to/change/directory/to");
    getcwd(cwd, sizeof(cwd));
    printf("Current working dir: %sn", cwd);
}
Wednesday, September 22, 2021
 
Smitha
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :