views:

54

answers:

3

Hello..

I am doing a project in PHP which I am not very familiar. I am using a MVC framework (CodeIgnitor). I have noticed that each time I return a view that resulted from a longer/shorter url string all of my includes break. It appears that the paths are relative to url.

Is $_SERVER["DOCUMENT_ROOT"] the best way to generate include paths in PHP?

Thanks!

A: 

I really depends on your application, I don't know how CodeIgnitor works, but here are a few points:

If you use the php path (defined in php.ini) you can always keep you includes in the php path, so including a file is no longer relative to the file path.

If you have a project directory (like /srv/www/myProject/) and all files you are using reside in this dir, then you could define a session value like this $_SESSION['project_path'] = '/srv/www/myProject' and then when including files, it would look like this:

include_once($_SESSION['project_path'] . 'included.php');

Calling the absolute path would make the include not care about the current path.

karlw
CodeIgnitor has url helper 'base_url()'. This seems to work nicely.
Nick
+1  A: 

You should read the User Guide on URL Helpers. It already has all the infos you need and provides with with functions that give you paths for your site.

If you need paths on your file system, there are BASEPATH, APPPATH and FCPATH. Look into index.php to see where they point (also has a description of these constants)

DrColossos
A: 

base_url() and site_url() is probably what you need.

stef