I actually use a resource controller to bring in all of my external files:
class Resources extends Controller
{
public function __construct()
{
parent::__construct();
}
public function javascript()
{
$arr = func_get_args();
if( sizeof( $arr ) == 0 )
{
show_404();
return;
}
if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
{
array_pop( $arr );
}
$name = implode( "/", $arr );
$this->load->view( "javascript", array( "importscript" => $name ) );
}
public function css()
{
$arr = func_get_args();
if( sizeof( $arr ) == 0 )
{
show_404();
return;
}
if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
{
array_pop( $arr );
}
$name = implode( "/", $arr );
$this->load->view( "css", array( "importscript" => $name ) );
}
public function image()
{
$arr = func_get_args();
if( sizeof( $arr ) == 0 )
{
show_404();
return;
}
if( is_numeric( $arr[ sizeof( $arr ) - 1 ] ) )
{
array_pop( $arr );
// if the last item is a number, that means it was
// automatically generated to prevent caching
}
$name = implode( "/", $arr );
$this->load->view( "images", array( "importscript" => $name ) );
}
}
The different views are all something like this:
$import = dirname( __FILE__ ) . "/javascript/$importscript";
if( !showjs( $import ) && is_dir( $import ) )
{
if( !showjs( "$import/$importscript" ) )
{
show_404();
}
}
function showjs( $imp )
{
if( is_file( "$imp.js" ) )
{
header('Content-type: application/javascript');
echo "/*----- Imported into PHP so JavaScript can all be dynamically declared -----*/\n\n";
echo file_get_contents( "$imp.js" );
return true;
}
elseif( is_file( "$imp.php" ) )
{
header('Content-type: application/javascript');
echo "/*----- Imported into PHP so JavaScript can all be dynamically declared -----*/\n\n";
include_once( "$imp.php" );
return true;
}
return false;
}
As you can see, the controller passes the file name to the view. The view then sees if there is a js file or a php file associated with the importscript variable. If there is, it sets the headers and then displays the files.