Created
March 14, 2012 01:37
-
-
Save codenamegary/2033264 to your computer and use it in GitHub Desktop.
Auto-Config function for laravel's index.php, sets 'LARAVEL_ENV' based on the active page URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
| Set a 'urls' (note the plural) in your application.php config files. | |
| Example application/config/application.php for PRODUCTION: | |
| | |
| return array( | |
| 'urls' => array( | |
| 'http://productionsite.com', | |
| 'http://www.productionsite.com', | |
| 'http://anothermirrordomain.com', | |
| 'http://www.anothermirrordomain.com', | |
| ), | |
| ); | |
| | |
| Example application/config/DEVELOPMENT/application.php for DEVELOPMENT: | |
| | |
| return array( | |
| 'urls' => array( | |
| 'http://localhost/site1/public', | |
| 'http://localhost:81/site1/public', | |
| 'http://localhost:8080/site1/public', | |
| ), | |
| ); | |
| | |
| Then you can use the function below to auto-detect the config that should be loaded | |
| based on the current URL. | |
| | |
| Goes into your index.php right after you've required paths.php. | |
| | |
*/ | |
// ... (SNIPPED CONTENTS OF INDEX.PHP) ... | |
// -------------------------------------------------------------- | |
// Set the core Laravel path constants. | |
// -------------------------------------------------------------- | |
require '../paths.php'; | |
// PASTE FUNCTION BELOW AFTER PATHS HAVE BEEN LOADED | |
array_map(function($path) | |
{ | |
// Only treat paths that have an application.php | |
if(strpos($path, 'application.php') !== false) | |
{ | |
// Every URL starts with http | |
$pageurl = 'http'; | |
// If it's a secure connection add 's' | |
if (isset($_SERVER['HTTPS'])) { | |
$pageurl .= ( $_SERVER['HTTPS'] !== 'off' || !empty($_SERVER['HTTPS']) ) ? 's' : ''; | |
} | |
// ... next comes the colon whack whack | |
$pageurl .= '://' . $_SERVER['SERVER_NAME']; | |
// ... and if it ain't port 80 add a :# (port) | |
if (isset($_SERVER['SERVER_PORT'])) { | |
$pageurl .= ( $_SERVER['SERVER_PORT'] != 80 ) ? ':' . $_SERVER['SERVER_PORT'] : ''; | |
} | |
// Set the base directory name of the config | |
$configdir = basename(dirname($path)); | |
// Load the array from the config file into $dirconfig | |
$dirconfig = array_merge(require $path); | |
// If URL is empty don't do anything, no need to change the LARAVEL_ENV var | |
if (!empty($dirconfig['urls'])) { | |
// If 'urls' in the config is just a key => value then make it an array. | |
$urls = (!is_array($dirconfig['urls'])) ? (array)($dirconfig['urls']) : $dirconfig['urls'] ; | |
foreach($urls as $url) { | |
if (strpos($pageurl,$url)===0) { | |
// We gots a match so change the LARAVEL_ENV to the name of the directory | |
// we are currently investigating. | |
$_SERVER['LARAVEL_ENV'] = $configdir; | |
} | |
} | |
} | |
} | |
}, iterator_to_array(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(path('app')."config")))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment