PHP function to dynamically switch paths
Hello friends I am coding a Joomla Template and I want to use an option of putting static files to a CDN.
I want the template 开发者_运维百科to look for the CDN path mentioned by user in the template options panel and if there is no input then it must take the files from the default local folder.
The local CDN folder is in the root of the template folder : templates/myTemplate/cdn
The structure inside the CDN folder is like this:
- cdn
---- css
---- images
---- js
So what exactly I am looking for is......
I call for a user input for the CDN of Path like this
$cdn_path = $doc->params->get("cdn-path","templates/myTemplate/cdn")
and get it via the templateDetails.xml file. Now user's input is.... http://mycdn.com/cdn
Here I need a function which takes the absolute path from user input (including the http://
) and add that as a value of the function CDNPath()
and if user does not inputs any value then it must add the default (templates/myTemplate/cdn)
as the value of CDNPath()
function CDNPath(){
<!-- What code should go here -->
return <!-- and here -->;
}
In my other functions for CSS, images and js paths I am using the following function
function CSSPath(){
return className::CDNPath().'css/';
}
function JSPath(){
return className::CDNPath().'js/';
}
function ImagePath(){
return className::CDNPath().'images/';
}
and in my template I link the files as:
<link rel="stylesheet"href="<?php echo $className->CSSPath(); ?>template.css" type="text/css" media="screen" />
<script type="text/javascript" src="<?php echo $className->JSPath(); ?>scripts.js"></script>
I am majorly seeing two challenges in this and that is of local and remote paths and what should be the exact code for that function.
This might get you started..
<?php
// CDN url from: $doc->params->get("cdn-path","templates/myTemplate/cdn")
$cdn_path = "http://www.google.com/images/";
// Local path, used if $cdn_path is not set
$local_path = "/images/";
// Retrieve our path
function get_path() {
// Bring in variables that were declared outside of the function
global $cdn_path, $local_path;
// If $cdn_path has a value, return it. Otherwise, return $local_path
return (isset($cdn_path) ? $cdn_path : $local_path);
}
// Use get_path() in any SRC attribute to retrieve the path
echo '<img src="' . get_path() . 'logo.png">' . PHP_EOL;
?>
精彩评论