如何在View for CodeIgniter 2.1中链接资产(图像,样式表等)? [英] How to link assets(images,stylesheet etc) in Views for CodeIgniter 2.1?

查看:41
本文介绍了如何在View for CodeIgniter 2.1中链接资产(图像,样式表等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter 2.1版&尝试通过使用 header.php 链接我的视图中的图像,样式表,javascript文件等资产:

I am using CodeIgniter Version 2.1 & trying to link assets like images,stylesheets, javascript files etc in my views by using, header.php:

< link href =<?php base_url();?> css / style.css rel = stylesheet />

我的控制器代码,调用视图:

my controller code, calls the view:

<?php
class Main extends CI_Controller{
    public function index() {
        $this->load->view('header');
}




  • 我要从中查看的视图文件加载资产位于
    ../ application / views / header.php

  • css文件已加载:
    ../ application / views / css / style.css

    • The view file from which I am trying to load the asset is located ../application/views/header.php.
    • the css file is loaded: ../application/views/css/style.css
    • 这不起作用。我收到 404-找不到页面错误。然后,我尝试将 css / style.css 移到 ../ application之外目录中的目录。令我惊讶的是,在webroot(站点../application/views)中拥有资产似乎很好。

      this does not work. I get 404 - Page not found error.then, I tried moving css/style.css outside ../application directory in webroot. To my surpise, having assets in webroot(outsite ../application/views) seems to work nicely.

      现在,
      我的问题是

      Now, My Question is


      1. 我们的资产是否直接位于webroot,站点 ../ application 目录中?如果是/否,那为什么呢?

      2. 如果直接在webroot中拥有资产是一个好主意,那么我应该移动 ../ application / views 目录吗?

      1. Is having our assets directly in webroot, outsite ../application directory right approach? If YES/NO, then why?
      2. If having assets directly in webroot is a good idea, than should I move ../application/views directory in webroot as well? How?

      PS:我是CodeIgniter框架的新手,所以不了解最佳实践

      PS: I am new to CodeIgniter framework, so unaware of the best practices

      推荐答案

      最好将资产放在webroot文件夹中。当从服务器请求资产时(取决于您的设置),它将从根目录开始并向下运行。

      It is best to put your assets in the webroot folder. When requesting assest from the server (depending on your setup), it will start from the root directory and work it's way down.

      http://yoursite.com/application/views/css/mystyles.css
      

      根目录,您只需要从那里进入

      if it's in the root you only need to go from there

      http: //yoursite.com/css/mystyles.css

      尽管值得将它们全部放在文件夹(/资产)中以保持它们的位置,以及编写更有效的重写规则的能力(如果您尝试从URL中删除index.php)可以忽略单个文件夹而不是所有单个文件夹(/ css,/ js等)

      Although it might be worth putting them all inside a folder (/assets) to keep them contained, as well as the ability to write a more effective rewrite rule (if you are trying to remove the index.php from the url) to ignore the single folder instead of all the individual folders (/css, /js, etc)

      对于views文件夹,最好将其保留在 application 文件夹中,因为CodeIgniter具有内置的加载程序,该加载程序会自动使用代码 $ this-> load-> vi时,检查 / application / views 中的视图文件ew('myview')

      As for the views folder, it's best if you leave it in the application folder as CodeIgniter has a built in loader that automatically checks /application/views for the view file when you use the code $this->load->view('myview')

      尽管可以移动views文件夹,但是如果您不熟悉CI,最好将其保留现在在那里。

      Although there are way to move the views folder, if you are new to CI it's probably best to leave it there for now.

      这篇关于如何在View for CodeIgniter 2.1中链接资产(图像,样式表等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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