Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I actually use a resource controller to bring in all of my external files:</p> <pre><code> 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-&gt;load-&gt;view( "javascript", array( "importscript" =&gt; $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-&gt;load-&gt;view( "css", array( "importscript" =&gt; $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-&gt;load-&gt;view( "images", array( "importscript" =&gt; $name ) ); } } </code></pre> <p>The different views are all something like this:</p> <pre><code>$import = dirname( __FILE__ ) . "/javascript/$importscript"; if( !showjs( $import ) &amp;&amp; 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; } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload