drd0833 2014-03-04 11:29 采纳率: 100%
浏览 117
已采纳

Codeigniter - 正确导入JS / CSS / IMG

I'm new in MVC learning and after searching for basic stuff, I found several opinions on this question/topic but everyone have different state and instructions...

Does anyone know what is the best place to put assets (js/css/img) files in Codeigniter ?

I found some guys stating to put it in the view folder, someone in the webroot (after creating an asset folder), someone inside the application folder etc so I really got confused.. Does it really depend on the user who is developing the application (so everyone put it in different places) or is there one basic "rule to follow" for that?

Can any expert in Codeigniter post the right answer and explain what is the best way to include those assets and why to do it like that?

  • 写回答

4条回答 默认 最新

  • dongyu6276 2014-03-04 12:46
    关注

    It comes down to taste, to some extent, but my humble suggestion is to use a basic bootstrap pattern which is followed by many frameworks, not just codeigniter. Look at how you would lay out your website based around a theme (https://github.com/twbs/bootstrap) for example.

    I don't claim to be expert, but I suggest something very simple and easy to understand for a team:

    Folder Structure

    /
    /css
    /js
    /application
    /bower_components
    ... 
    

    Theme Layout

    When working with a page, I don't want to have to think about loading the various libraries and page content, I just want to work simply with the page. To do this, I split the content so I can load it with the following calls:

    $this->load->view('site/header'); //Load the header and all the appropriate libs
    
    $this->load->view('/users/edit', array('
       'record' => $this->User->get($userid);   //Work with the actual page
    '));
    
    $this->load->view('/site/footer'); //Close all the tags
    

    Typically, I lay the libraries in the root, then load them in a view using absolute paths:

    /application/views/site/header.php:

    <html>
    ....
    <head>
     <script src="/js/bootstrap.min.js"></script>
     <link href="/css/bootstrap.css" rel="stylesheet">
     </head>
    ...
    <body>
     <navbar>
      ...
     </navbar>
    <div id="content">
    

    /application/view/site/footer.php

     </content>
     ... closing tags
     </html>
    

    Repo example

    See a blank repo setup with free theme here: https://github.com/DesertLynx/blank_CI

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型