Request for feedback: require_once() code generator alternative
1 posts by 1 authors in: Forums > CMS Builder
Last Post: April 5, 2010 (RSS)
By Dave - April 5, 2010 - edited: April 6, 2010
Hello All,
This thread is about a change I'm thinking about for the code generator. Please read and post any feedback, be it good, bad or indifferent.
The viewer pages need to load a CMSB library to function and they usually do that with a line like this:
The problem with that is that it only works on one server because of the "absolute path". You can switch it to a "relative path" like the following example, but it's hard to explain to new users, can't be done automatically, and requires you know where the files are relative to each other.
We manually do this on all the sites we build so they work in our local development environment and live as well.
If you use the first method, though, and you run it locally or on a different server you can an error like this:
Warning: require_once(C:/wamp/www/sb/CMS Builder/cmsAdmin/lib/viewer_functions.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\sb\CMS Builder\cmsAdmin\_testViewers\newsList.php on line 5
Which we've been asked about and answered many times. So I came up with this code. Basically it checks the full path CMSB was running in, but if it can't find the files there it looks in the current directory, and up to 3 parent directories.
It's a few more lines, but much more portable. Please post any comments or feedback, or try swapping your require_once() code with this and let me know if it works for you.
Thanks!
This thread is about a change I'm thinking about for the code generator. Please read and post any feedback, be it good, bad or indifferent.
The viewer pages need to load a CMSB library to function and they usually do that with a line like this:
require_once "C:/wamp/www/sb/CMS Builder/cmsAdmin/lib/viewer_functions.php";
The problem with that is that it only works on one server because of the "absolute path". You can switch it to a "relative path" like the following example, but it's hard to explain to new users, can't be done automatically, and requires you know where the files are relative to each other.
require_once "cmsAdmin/lib/viewer_functions.php";
We manually do this on all the sites we build so they work in our local development environment and live as well.
If you use the first method, though, and you run it locally or on a different server you can an error like this:
Warning: require_once(C:/wamp/www/sb/CMS Builder/cmsAdmin/lib/viewer_functions.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\sb\CMS Builder\cmsAdmin\_testViewers\newsList.php on line 5
Which we've been asked about and answered many times. So I came up with this code. Basically it checks the full path CMSB was running in, but if it can't find the files there it looks in the current directory, and up to 3 parent directories.
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('C:/wamp/www/sb/CMS Builder/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
It's a few more lines, but much more portable. Please post any comments or feedback, or try swapping your require_once() code with this and let me know if it works for you.
Thanks!
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com