Jump to content

Recommended Posts

Say I have directory setup like this.

 

core/

includes/

snippets/

   template.php

js/

index.php

 

Say in the index.php file, I have a file path like this " include 'snippets/template.php'; ". It works.

 

Or say If I am in template.php and I have a file path like this " include '../index.php'; ". It works as well.

 

I am wondering if there is a more global method to this so I don't have to worry about when to use "../" or not. It becomes hectic if there are a lot of includes and/or links.

Putting your entire application into the document root is not a great idea. It means that every internal script can be accessed directly, which may lead to unexpected side effects. And if your webserver ever fails to handle .php files correctly, maybe just for a few minutes, then the application suddenly becomes downloadable, including all your configuration data (passwords, keys etc.).

 

The document root is for public resources like your index.php, images, CSS, JavaScript etc. But all internal files (classes, functions, templates, configuration files, ...) belongs into a separate directory outside of the document root.

 

The approach I take is to have the webserver set an environment variable with the path to the internal application directory. Then the index.php can load an initialization script which sets up the autoloaders and/or helper functions for script inclusion.

<?php

// get environment variable with the path of the internal web application directory
$app_base_dir = getenv('APP_BASE_DIR');
if (!$app_base_dir)
{
    throw new RuntimeException('Environment variable APP_BASE_DIR is not set.');
}

// include initialization script which sets up autoloaders, defines helper functions for script inclusion etc.
require_once $app_base_dir.'/bootstrap.php';



// now you can just start creating classes
$router = new \YourCompany\Core\Router();

//  or if you're into oldschool PHP, include scripts
include_template('navigation.php');    // the function will take care of resolving the path, e. g. /path/to/app/templates/navigation.php
include_functions('html');

But manual script inclusion should be getting rare these days, anyway. Thanks to autoloaders and template engines, PHP can do that all by itself once you've provided a few base directories.

  • Like 1

If you can't or don't want to mess with your server configuration, an alternative is to infer the application root based on the location of your index.php file using the __DIR__ magic constant.

 

$app_base_dir = realpath(__DIR__ . '/..');
Assuming your index.php is in your document_root, then $app_base_dir would point to it's parent directory. The call to realpath() isn't really necessary but I like to do it.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.