Skip to content

Instantly share code, notes, and snippets.

@serapheem
Last active December 20, 2015 01:09
Show Gist options
  • Select an option

  • Save serapheem/6047491 to your computer and use it in GitHub Desktop.

Select an option

Save serapheem/6047491 to your computer and use it in GitHub Desktop.
Symfony2 - Getting Base and Root Paths
<?php
// Sometimes you want to be able to get the base path url for your project
// and also another times you just want to get the root folder where
// the symfony2 application is living.
// For the first one, the url base path you get it this way:
$this->get('request')->getBasePath();
// So when loading a page with URL http://localhost/Symfony/web/app.php
// of a symfony2 project located at /var/www/Symfony/ you just get:
/var/www/Symfony/web
// On the other hand, if you wanted to get the root, that is the filesystem
// path to the root app dir of the application then you would issue something
// like this:
$this->get('kernel')->getRootDir();
// Which will return for our case this value:
/var/www/Symfony/app
// Notice this is the app folder and not the root folder per say.
// The app folder is where the autoload and AppKernel are living.
// With that in mind you can navigate below that directory but it is not really
// needed as you would probably be doing things into your app folder anyways.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment