开发者

Using a controller to handle returning customized css & javascript files with codeigniter

I'm working on a php/codeigniter project and I'm thinking about creating a controller specifically to handle returning customized css & javascript files.

In previous projects I've included external CSS & JS files in the headers of my view files but they've essentially had to be static since they were just sent as regular assets by the server. For my next project I'm thinking about using a controller and essentially mak开发者_开发技巧ing my CSS/Javascript files 'views' that are loaded by the controller.

What do you guys think? Is this a good approach? Is there a generally accepted better way of doing this?


By using a controller for your js/css, you have the overhead of loading a ton of unnecessary stuff for every request for the file. The security class, the input class, the router, exceptions, etc.

You don't need any of this for what should be static content. You don't want the files to be dynamic either (changing per request) because this will affect the browser cache. Either the file will get cached in the state it was loaded, or you will lose the benefit of the browser cache.

Your best bet is to combine the files, minimize them, and write the content to a static file, but then they aren't dynamic. However, they probably shouldn't be.

Another thing you can do is to use a php file to combine the static files and reference it in your <link> tag (screen.php). Simple CSS example:

<?php
header('Content-type: text/css');
$files = array(
    'css/reset.css',
    'css/screen.css',
    'css/typography.css',
);
foreach ($files as $file)
{
    readfile($file);
}

Either way, you should update the file name, perhaps with a query string, in order to avoid the old cached file being served. Example: /screen.css?version=2

I do use $this->load->view() sometimes for dynamic js, but only for inline script tags (dynamic WYSIWYG configuration for example). This allows you to pass variables to the "view" file as well as avoid cache issues.

My point of view is that it's not worth firing up CI and all the related dependencies just to serve what should be static content.


I don't know if there's an "official" way of doing that, I usualy split a page into different views, one of which is the page head. If you do something like

class mycontroller extends CI_Controller {

function index()
{
   $data['css'] = 'style1.css';
   $data['js'] = 'custom.js';
   $this->load->view('head',$data);
   $this->load->view('body');
   $this->load->view('foot')  // where I close the </html> tag, for example
}

}

You can pass whatever value and, in your view, just load what is passed by the controller

<!DOCTYPE>
<html>
<head>
<?php echo link_tag(base_url().'css/'.$css);?>
<script type="text/css" src="<?php echo base_url();?>js/<?php echo $js;?>"></script>
</head>


Something I've done before is I have a standard list of "always used" css/js files then each controller can include their own specific ones. Within the controller I set an array of "includes" for js/css then the header view checks for the array and implodes it. This way all the needed js/css files are specified in the head tag.

Example from controller:

$data['includes']['js'] = array('<script type="text/javascript" src="' . base_url() . 'assets/js/script.js"></script>');

Example from view:

if(isset($includes['js'])) echo implode("\n", $includes['js']) . "\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜