使用控制器来处理返回的自定义 css 和带有 codeigniter 的 javascript 文件

发布于 2024-11-18 23:09:44 字数 272 浏览 0 评论 0原文

我正在开发一个 php/codeigniter 项目,我正在考虑创建一个专门用于处理返回自定义 css 和 css 的控制器。 JavaScript 文件。

在之前的项目中,我已经包含了外部 CSS 和JS 文件位于我的视图文件的标头中,但它们本质上必须是静态的,因为它们只是由服务器作为常规资源发送。对于我的下一个项目,我正在考虑使用控制器,并本质上使我的 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-11-25 23:09:44

通过为 js/css 使用控制器,您需要为每个文件请求加载大量不必要的内容。安全类、输入类、路由器、异常等。

对于静态内容,您不需要任何这些。您也不希望文件是动态的(根据请求更改),因为这会影响浏览器缓存。文件要么会以加载时的状态被缓存,要么您将失去浏览器缓存的好处。

最好的选择是合并文件,最小化它们,并将内容写入静态文件,但它们不是动态的。然而,他们可能不应该这样。

您可以做的另一件事是使用 php 文件来组合静态文件并在 标记 (screen.php) 中引用它。简单的 CSS 示例:

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

我有时会使用 $this->load->view() 来处理动态 js,但仅限于内联脚本标记(例如动态所见即所得配置)。这允许您将变量传递到“视图”文件并避免缓存问题。

我的观点是,仅仅为了提供静态内容而启动 CI 和所有相关的依赖项是不值得的。

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.

彩虹直至黑白 2024-11-25 23:09:44

我不知道是否有“官方”方法可以做到这一点,我通常将页面分成不同的视图,其中一个是页面标题。
如果您执行类似的操作,

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
}

}

您可以传递任何值,并且在您的视图中,只需加载控制器传递的内容

<!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>

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>
拍不死你 2024-11-25 23:09:44

我之前做过的事情是我有一个“始终使用”的 css/js 文件的标准列表,然后每个控制器都可以包含自己的特定文件。在控制器中,我为 js/css 设置了一个“包含”数组,然后标头视图检查该数组并将其内爆。这样,所有需要的 js/css 文件都在 head 标记中指定。

控制器示例:

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

视图示例:

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文