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

十年热恋 提交于 2019-12-13 17:04:16

问题


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 making 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?


回答1:


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.




回答2:


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>



回答3:


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";


来源:https://stackoverflow.com/questions/6613376/using-a-controller-to-handle-returning-customized-css-javascript-files-with-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!