Multiple Server Config

It is common for developers to work in multiple environments and many start locally, update on a staging server so the clients can see the progress and when ready to launch they set up on the live server.

It is nice to have one single config file to cater for all these environments so you don’t have to keep editing your config file. This also ensures that you don’t forget to update a file on one of these environments to enable certain options.

When you first set up Perch locally, the generate config.php file might look like the following. For the sake of simplicity we’ll assume you use localhost locally.

switch($_SERVER['SERVER_NAME']) {
        case 'localhost':
                include(__DIR__.'/config.localhost.php');
                break;

        default:
                include('config.production.php');
                break;
}

define('PERCH_LICENSE_KEY', 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX');
define('PERCH_EMAIL_FROM', '[email protected]');
define('PERCH_EMAIL_FROM_NAME', 'Hussein Al Hammad');

define('PERCH_LOGINPATH', '/perch');
define('PERCH_PATH', str_replace(DIRECTORY_SEPARATOR.'config', '', __DIR__));
define('PERCH_CORE', PERCH_PATH.DIRECTORY_SEPARATOR.'core');

define('PERCH_RESFILEPATH', PERCH_PATH . DIRECTORY_SEPARATOR . 'resources');
define('PERCH_RESPATH', PERCH_LOGINPATH . '/resources');

define('PERCH_HTML5', true);
define('PERCH_TZ', 'UTC');

What does this all mean? You may notice that Perch has created 2 other files: config.localhost.php and config.production.php, one for your local environment and one for your production environment respectively.

The switch statement checks how you are accessing Perch and based on that includes a file. As is, if you are connecting through localhost, config.localhost.php is included and in all other cases config.production.php is included. And by including the file Perch will use the configurations on there as well as the ones on config.php.

What we have is basically:

Now when you want to set up in a new environment, you don’t have to overwrite your config for the other ones. You add a new case:

switch($_SERVER['SERVER_NAME']) {
        case 'localhost':
                include(__DIR__.'/config.localhost.php');
                break;

        case 'staging.example.net':
                include(__DIR__.'/config-staging-example-net.php');
                break;

        default:
                include('config.production.php');
                break;
}

Teams:

Working in a team can also mean you work in a different set up (server name, DB username etc) from your team members. It’s a good idea to share the same set up in a team, but if you must have different set ups, you can add another case to cater for each different local environment.

Same network access:

You may need to access the website from another device when in development for testing. It would also be handy to be able to show your boss your progress on another device (perhaps at a meeting). You can add another case with the internal IP address to handle this:

switch($_SERVER['SERVER_NAME']) {
        case 'localhost':
                include(__DIR__.'/config.localhost.php');
                break;

        case '192.168.10.136':
                include(__DIR__.'/config.localhost.php');
                break;

        case 'staging.example.net':
                include(__DIR__.'/config-staging-example-net.php');
                break;

        default:
                include('config.production.php');
                break;
}

And if you work on more than one network/device (e.g. desktop/laptop, office/home/cafe), chances are you won’t always have the same internal IP address. It would be annoying to change your config file just to be able to test on mobile while you’re at a cafe and change it again when you are working at your office.

In this case you can use getHostByName(getHostName()):

switch($_SERVER['SERVER_NAME']) {
        case 'localhost':
                include(__DIR__.'/config.localhost.php');
                break;

        case getHostByName(getHostName()):
                include(__DIR__.'/config.localhost.php');
                break;

        case 'staging.example.net':
                include(__DIR__.'/config-staging-example-net.php');
                break;

        default:
                include('config.production.php');
                break;
}

Same network access is extremely useful if you want to test on real devices. And if you are using Gulp workflow for Perch, you can even sync scrolls and clicks on multiple browsers and devices at once!

Besides if you use CrossBrowserTesting (not a sponsor, I really like their service), you can enable local connection and test on all sorts of browsers and devices. The local connection requires you to enter an internal IP address so catering for same network access is necessary here too!

link

Related articles