4

I have been trying to improve my dev/live workflow recently to get 1:1 parity. I want everything to work the same on Dev (my local machine) as it does on live (the Web). Aside from having a different config file (containing different mySQL credentials) and a different .htaccess file, I want the rest of the site to work exactly the same on my live environment as it does on my dev environment. The problem is I cannot get my include paths to resolve the same way on both environemnts.

I have all of my includes configured in this format:

Include path

$_SERVER["DOCUMENT_ROOT"] . /website1/includes/file.php 

The problem is that on my LIVE server, $_SERVER["DOCUMENT_ROOT"] does not contain the /website1/ part, but on my TEST server, it does. That means the paths resolve differently on the two environments:

Live:

    /some/root/folder/website1/includes/file.php

Dev:

    /some/root/folder/website1/website1/includes/file.php

I've worked around this somewhat by copying the included files into a subfolder called /website1 for use in the MAMP dev environment. But this is clearly not ideal as it adds unnecessary overhead. Whenever I edit one of the included files, I have to be sure to edit the file in both locations.

Does anyone have any suggestions for making the include paths the same on both environments, without duplicate files? I have tried using dirname(__FILE__), but that would require making alot of changes so that relative paths can resolve. I tried various other solutions such as trying to change the include path via .htaccess, httpd.conf or php.ini, but nothing seems to be working. I know there must be a simple solution, but I'm lost. Ideally I'd like any configuration change to be to the local environment so as to avoid tampering with the live environment.

Much appreciated!

2 Answers2

4

There are multiple solutions but I propose these 2 below.

* Option A *

1 main config file which includes a specific environment file.

<?php
// main config
define('ENV','dev'); // or define('ENV','prod');

require_once __DIR__ . '/config.' . ENV . '.php';

// ...

Environment specific file for development:

<?php
// config.dev.php
$path_to_includes = '/some/root/folder/website1/website1/includes';

// ...

Environment specific file for production:

<?php
// config.prod.php
$path_to_includes = '/some/root/folder/website1/includes';

// ...

In php scripts:

<?php
require_once "$path_to_includes/file.php";

// ...

* Option B *

Move your development folder to match production structure.

$ mv /some/root/folder/website1 /some/root/folder/website.old
$ mv /some/root/folder/website.old/website1 /some/root/folder/website1
$ rm /some/root/folder/website.old

* Option C *

Set your apache2 DocumentRoot to the right value. For development:

DocumentRoot /some/root/folder/website1

For production:

DocumentRoot /some/root/folder

After all, I recommend you to have all environments exactly the same, and parametrize it with the right values in config files. Having the same project structure helps simplifies debugging problems and is for that reason that my preferred Solution is Option B.

R.Sicart
  • 671
  • 3
  • 10
  • For option B, my apache virtual host will not resolve if I move the files. But if I change the document root, the problem will come back again. ServerName website1.dev ServerAlias www.website1.dev DocumentRoot "/my/path/to/website1" – AardvarkApostrophe Jul 10 '14 at 10:19
  • For option 1, it looks like every php file would have to be changed. Is that the case? – AardvarkApostrophe Jul 10 '14 at 10:20
  • Yes, to use the new $path_to_includes variable. – R.Sicart Jul 10 '14 at 20:48
  • For Option b your apache2 virtualhost should work. You could also try Option C in updated comment. – R.Sicart Jul 10 '14 at 20:53
  • I have the same issue as I'm developing a site on "www.site1.com/subfolder/"(subfolder on my own website host) and pushing to "www.site2.com" (client website root). Option A seems the only one that will work for me so what's the best way to ensure I always link to the correct config file or do I just have to be extremely careful when deploying? – nickstaw Nov 05 '15 at 10:41
0

In my case, my dev server is on Windows and my prod server is on Linux.

My first solution, years ago, was to add customizing code in a file and include the file on each page, based on whether the server IP address was 127.0.0.1. This was messy, and I was never really happy with it.

My second solution was to put a file named DEVELOP in each page's directory, and no such file on the prod system. But this had the overhead of file_exists() and proliferating DEVELOP files.

My third solution was to use the SetEnv Apache directive on both servers to customize the environment as needed. But this requires using getenv() or apache_getenv() in all pages needing the customizations.

Then I realized that the main system-wide customization that I really needed was to enable all PHP programs in all of my websites to access a common directory containing commonly-used files. (Until this point I was uploading a copy of the common directory into each website's root. I had to keep them synchronized. Ugh.)

Providing this common access can be achieved simply by editing the include_path directive in the php.ini file for each server to have the (different) absolute pathname of the common directory (at the same time I eliminated the period (.) from the include path, since this just creates extra overhead).

Now I can strip out all that clumsy customization code from my many php files (not so hard using search and replace tools), and write references to common files as though they were in the current dir: 'require "standard.php";'. Fortunately, none of the names of the common files or directories are expected to be the same as those of existing files in any of my websites. If they were, then the version in the current dir would take precedence over the common file of the same name with no error message.

It would have been nice to have been able to specify the parent of the common directory in the include path, so that include statements could reference "common/standard.php" to ensure no name conflicts, but this would make all the files and directories in the parent accessible by name in every website on the server!

Added: I wonder if I could setup the common directory like this: /www/commonparent/common? I would add "/www/commonparent" to the include path, and use references in programs like "common/standard.php". This would be fairly safe, since name conflicts are eliminated (the commonparent directory contains no names except for "common"). I'm going to try this.

David Spector
  • 1,520
  • 15
  • 21