dongroufan6846 2016-06-23 20:39
浏览 47

CodeIgniter中的图像文件,CSS和JS文件

I started to work with CodeIgniter today! So i got 2 simple questions!

Question 1 There is any way to automate the way to reference images in php code, instead to writing img/mainpage/folder.png everytime?
Can i set a global variable with path of img files and use this in all pages? Example: set $IMGPATH = '..\img\'. And use something like <img src='$IMGPATH\myimage.png' in all pages?

Question 2 There is any way to load my JS and CSS files just one time and let it avaliable for all pages instead to stay loading JS and CSS files on every page header?
How can i optimze my CSS and JS files using CodeIgniter and load it ONE TIME and let it avaliable for all pages?

  • 写回答

3条回答 默认 最新

  • douhongxie5436 2016-06-23 20:49
    关注

    Question 1:

    You can create a class with static member that will hold the path.

    class Path {
      public static $path = "path/to/directory";
    }
    

    Then in your code you can call <img src='<?=Path::$path?>/myimage.png' />

    Question 2:

    It would be possible using frames, but that's really bad idea. Better approach is using caching. If you use apache, you can enable mod_expires and then use something like this:

    <IfModule mod_expires.c>
      ExpiresActive On 
      ExpiresByType text/css "access plus 1 week"
      ExpiresByType text/javascript "access plus 1 week"
    </IfModule>
    

    This means that the browser will load css and javascript files from internet only once and then it will load them from cache (which is extremely fast) for a week. After the week it will load them again from internet and so on.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向