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条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制