Jump to content

What's the best way to have a global url file path in a website?


imgrooot

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.