使用控制器处理返回的定制css& javascript文件与codeigniter [英] Using a controller to handle returning customized css & javascript files with codeigniter

查看:91
本文介绍了使用控制器处理返回的定制css& javascript文件与codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个php / codeigniter项目,我正在考虑创建一个控制器专门处理返回自定义css& javascript文件。

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

在以前的项目中,我包括了外部CSS& JS文件在我的视图文件的头文件,但他们本质上必须是静态的,因为他们只是作为常规资产发送服务器。对于我的下一个项目,我正在考虑使用一个控制器,并且基本上使我的CSS / Javascript文件的视图由控制器加载。

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?

推荐答案

通过使用控制器的js / css,为文件的每个请求加载一吨不必要的东西。安全类,输入类,路由器,异常等。

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.

另一件你可以做的是使用一个php文件来组合静态文件,并引用它在你的< link> 标记(screen.php)。简单的CSS示例:

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);
}

无论如何,你应该更新文件名,以避免提供旧的缓存文件。示例: /screen.css?version=2

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

我使用 $ this- > load-> view()有时用于动态js,但仅用于内联脚本标记(例如动态WYSIWYG配置)。这允许你传递变量到视图文件,以及避免缓存问题。

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.

我的观点是,不值得启动CI和所有相关依赖关系只是为了应该是静态内容

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.

这篇关于使用控制器处理返回的定制css&amp; javascript文件与codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆