-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Asset Manager
[b] So I was checking out some of the assets extensions people have made and though they were useful and served their purpose, I had some additions I wanted to make and so I went off to make my own asset system. Here is the scoop: [/b]
[b] So I wanted to be able to use the CodeIgnitor system in my javascript and css files and I wanted a simple way to incorporate this as well as load images, js, css, swf, flv, etc... So my system is as follows. [/b]
[b] The files used are: [/b]
[b] application>config>assets.php application>controllers>asset_controller.php application>libraries>Assets.php [/b]
[h3] And then just add in a simple route in our application>config>routes.php file. [/h3]
[code] $route["asset/:any"] = "asset_controller/index"; [/code]
[h3] application>config>assets.php [/h3]
[code] <?php $config["assets_folder"] = "assets"; $config["assets_path"] = $_SERVER["DOCUMENT_ROOT"]."/".$config["assets_folder"]; $config["default_level"] = "public";
// set asset types and folders $config["asset_types"] = array( "flv" => "flash", "swf" => "flash", "jpg" => "images", "png" => "images", "gif" => "images", "js" => "js", "css" => "css" ); ?> [/code]
[h3] application>controllers>asset_controller.php [/h3]
[code] <?php
class Asset_controller extends Controller {
function Asset_controller()
{
parent::Controller();
}
function load_files(){
$this->load->config("assets");
$asset_folder = $this->config->item("assets_path");
$type = $this->uri->segment(2);
$level = $this->uri->segment(3);
$files_path = "$asset_folder/$level/$type/";
$ext = ".$type";
$file = $this->uri->segment(4);
if($type == 'js'){
Header("Content-type: text/javascript");
}elseif($type == 'css'){
Header ("Content-type: text/css");
}
require_once($files_path.$file.$ext);
}
} ?> [/code]
[h3] application>libraries>Assets.php [/h3]
[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Assets {
var $javascript = array();
var $css = array();
function Assets($config=array()){
// Get CI instance
$this->ci =& get_instance();
if(!empty($config)){
foreach($config as $k => $v){
$this->$k = $v;
}
}else{
show_error("Assets Config File is Missing or is Empty");
}
}
function load($file,$level="",$params=array()){
if(!$level){
$level = $this->default_level;
}
$type = strtolower(substr(strrchr($file, '.'), 1));
if($type=="jpg" || $type=="png" || $type=="gif"){
// Generate Image Link
$image_link = site_url("$this->assets_folder/$level/images").$file;
// Generate the Paramaters
$image_params = $this->generate_params($params);
// Create Image Tag
$output = "<img src='$image_link'$image_params />";
return $output;
}elseif($type=="js"){
if(array_key_exists("extra",$params) && $params["extra"] != ""){
$this->javascript[] = "$type/$level/$file/{$params["extra"]}";
}else{
$this->javascript[] = "$type/$level/$file";
}
}elseif($type=="css"){
$this->css[] = "$type/$level/$file";
}else{
return false;
}
}
function url($file,$level=""){
if(!$level){
$level = $this->default_level;
}
$type = strtolower(substr(strrchr($file, '.'), 1));
if(array_key_exists($type,$this->asset_types)){
foreach($this->asset_types as $asset_type => $folder){
if($type == $asset_type){
$output = site_url("$this->assets_folder/$level/$folder").$file;
return $output;
break;
}
}
}else{
show_error("$type is not a valid asset type");
}
}
function generate_params($params){
$output = '';
if(!empty($params)){
foreach($params as $k => $v){
$output .= ' '.$k.'="'.$v.'"';
}
}
return $output;
}
function display_header_assets(){
$output = '';
foreach($this->javascript as $file){
$output .= "\n";
}
foreach($this->css as $file){
$output .= "<link type='text/css' rel='stylesheet' href='".site_url("asset").str_replace(".css","",$file)."/' />\n";
}
return $output;
}
}
?> [/code]