Prefix URL

6 posts by 4 authors in: Forums > CMS Builder
Last Post: June 29, 2014   (RSS)

By Toledoh - June 26, 2014

Hi All,

How do I use :<?php echo PREFIX_URL ?> inside <?php require_once("foldername/filename.php"); ?>

ie: <?php require_once("PREFIX_URL/foldername/filename.php"); ?>

Cheers,

Tim (toledoh.com.au)

By Steve99 - June 27, 2014

Hey Tim,

<?php require_once(PREFIX_URL."/foldername/filename.php"); ?> would be how it's used, however I believe that will give you a stream error if used within a file in your /dev/ folder because prefix url must start with / however it should be fine in a subfolder of /dev/ like /dev/subfolder/. This is assuming it's a situation similar to having a live site in the web root and developing a new one in a subfolder called /dev/ (ex. livesitedomain.com, livesitedomain.com/developmentFolder/, livesitedomain.com/developmentFolder/subfolder/)

For includes in files within /developmentFolder/, you could perform an ltrim on the PREFIX_URL to remove leading slash and set that variable.

<?php

$prefixDevFolder = ltrim(PREFIX_URL, '/');

require_once($prefixDevFolder."/foldername/filename.php"); 

?>

Give that a try. Hope this helps.

Cheers,

Steve

By Toledoh - June 27, 2014

Thanks Guys,

I'm still getting errors:

Warning: require_once(~murray/agelock/agelock.php): failed to open stream: No such file or directory in /home/murray/public_html/assets/includes/_header.php on line 5 Fatal error: require_once(): Failed opening required '~murray/agelock/agelock.php' (include_path='/home/murray/public_html/cmsAdmin:/home/murray/public_html/cmsAdmin/3rdParty:.:/usr/lib/php:/usr/local/lib/php') in /home/murray/public_html/assets/includes/_header.php on line 5

It's working in most cases to have: <?php require_once("agelock/agelock.php"); ?> as most files for the site are in the root folder (currently staging at ~murray) and the relative link works fine.

However, The shopping cart for the site is in the folder ~murray/shop, so the relative link doesn't work.

I was hoping to use the prefix URL to fix that, so the resulting code would always be /~murray/agelock/agelock.php

It seems though, that it doesn't like the /~murray/ in the include - as I'm sure the URL is correct.

Cheers,

Tim (toledoh.com.au)

By Dave - June 27, 2014

Hi Tim, 

Often when prefix urls such as ~username are added in the web urls they don't match the naming structure in the file system so they're not always suitable for using in filepath includes.

For includes in the /shop/ folder you could try this: <?php require_once("../agelock/agelock.php"); ?>

Or you could try $_SERVER['DOCUMENT_ROOT'] which is (mostly) set correctly: <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/agelock/agelock.php"); ?>

Or you could use CMSB's SCRIPT_DIR constant which has the path to the cms.  You'll need to go up a folder from there though with ../.  Eg: <?php require_once(SCRIPT_DIR . "/../agelock/agelock.php"); ?>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Toledoh - June 29, 2014

<?php require_once($_SERVER['DOCUMENT_ROOT'] . "/agelock/agelock.php"); ?> works fine - thanks Dave!

Cheers,

Tim (toledoh.com.au)