php 如何在codeigniter中加载css文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20697954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:26:03  来源:igfitidea点击:

how to load css file in codeigniter

phpcsscodeigniter

提问by user3011896

Hi Frnds i cannot able to load css file in codeigniter. so please tell me how to load. Codeigniter sample Directory Structure

嗨 Frnds 我无法在 codeigniter 中加载 css 文件。所以请告诉我如何加载。Codeigniter 示例目录结构

  1. views
  2. models
  3. controllers
  4. assets a) stylesheets b) javascript c) images
  5. Config
  1. 意见
  2. 楷模
  3. 控制器
  4. 资产 a) 样式表 b) javascript c) 图像
  5. 配置

Controller:

控制器:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Learnersway extends CI_Controller
    {
        function index()
        {
            $this->load->view('home_learnersway');
        }
    }
    ?>

View

看法

    <html>
    <head>

        <link rel="stylesheet" type="text/css" href=“assets/stylesheets/main.css”   />

    </head>
    <body>
    <div class="wrapper">
    <div class="header">
    <div class="title"><img height="136" src="../images/learnersway.jpg" width="272" /></div>

    <div class="main_menu">
    <ul>
        <li>HTML</li>
        <li>XML</li>
        <li>CSS</li>

    </ul>
    </div>
    </div>

    <div class="main_container">
    <div class="sub_menu">
    <ul>
        <li>HTML Introduction</li>
        <li>HTML Editors</li>
        <li>HTML Basic</li>
        <li>HTML Elements</li>
        <li>HTML Attributes</li>
        <li>HTML Headings</li>
        <li>HTML Paragraphs</li>
        <li>HTML Formatting</li>
        <li>HTML Links</li>
        <li>HTML Head</li>

    </ul>
    </div>

    <div class="sub_container">
    <div>Under Construction...</div>
    </div>
    </div>

    <div class="footer">
    <div class="left">All Rights Reserved @ LearnersWay.com</div>

    <div class="right">
    <ul>
        <li>About Us</li>
        <li>Terms &amp; Privacy Policy</li>

    </ul>
    </div>
    </div>
    </div>
    </body>
    </html>

Note:

笔记:

Please tell me how to load css file in codeigniter. Even if can tell me how load javascripts and images also. I am a beginner in codeigniter MVC Framework.

请告诉我如何在 codeigniter 中加载 css 文件。即使可以告诉我如何加载 javascripts 和图像。我是 codeigniter MVC 框架的初学者。

回答by lyhong

First go to application/config/autoload.php. Then add $autoload['helper'] = array('html','url');

首先去application/config/autoload.php。然后加$autoload['helper'] = array('html','url');

<link rel="stylesheet" href="<?php echo base_url('assets/stylesheets/main.css')?>"/>
//OR
<?php echo link_tag('assets/stylesheets/main.css')?>

//Image
<?php echo img('asset/images/learnersway.jpg')?>
//OR
<img src="<?php echo base_url('asset/images/learnersway.jpg')?>" />

//Javascript
<script src='asset/javascript/yourscript.js'></script>

Please visit userguide http://ellislab.com/codeigniter%20/user-guide/helpers/html_helper.html

请访问用户指南http://ellislab.com/codeigniter%20/user-guide/helpers/html_helper.html

回答by Hugo B.

There are many ways to include css-files, e.g.:

有很多方法可以包含 css 文件,例如:

Load this helper: $this->load->helper('url');and then you can use this in your view:

加载这个助手:$this->load->helper('url');然后你可以在你的视图中使用它:

<link rel="stylesheet" type="text/css" href=“<?php echo base_url(); ?>path/to/css-file">

or

或者

<link rel="stylesheet" type="text/css" href=“<?php echo base_url('path/to/css-file'); ?>">

回答by user2936213

In your view do use site_url();to load css,js and images as:

在您看来,请使用site_url(); 将 css、js 和图像加载为:

<link rel="stylesheet" type="text/css" href=“<?php echo site_url();?>assets/stylesheets/main.css”  
<script type="text/javascript" language="javascript" src="<?php echo site_url(); ?>assets/javascript/yourjs.js"></script>

Put this both in headtag and wherever you want to show images use this:

把它放在head标签和任何你想显示图像的地方使用这个:

<img title="yourimagetitle"src="<?php echo site_url();?>assets/images/yourimage.extension" height="100" width="200">

回答by Sandeep Maurya

use below lines in your template file

在模板文件中使用以下几行

<link rel="stylesheet" href="<?php echo $base?>/application/css/style.css" type="text/css">

and load this helper file $this->load->helper('url')

并加载这个帮助文件 $this->load->helper('url')

回答by putranoval

you can use my answer on this question how to load css, js dynamically in codeigniter

您可以在这个问题上使用我的回答 如何在 codeigniter 中动态加载 css、js

    public function index() { 
// View "css_js_view" Page.
 //conditional 
if(your condition){
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 }else{
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 } 
$this->load->view('load_view',$this->data);

}

than on your view

比在你看来

<!--Load css and js--> 
    <link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
     <script src="<?php echo $js; ?>"></script>

also you can ue this tutorial How to load css,javascript conditionally?

您也可以使用本教程如何有条件地加载 css、javascript?

回答by Towsif

The best practice in codeigniter for set up the assets as below flow:

codeigniter 中设置资产的最佳实践如下流程:

  • application
  • assets
  • system
  • 应用
  • 资产
  • 系统

But you want the flow as below:

但你想要的流程如下:

  • application > assets > css > style.css
  • system
  • 应用程序 > 资产 > css > style.css
  • 系统

So if you want to keep the assets folder inside the application, then you've to do some extra functionality as below: First, make sure that you've loaded the URL helper in your controller which is:

因此,如果您想将 assets 文件夹保留在应用程序中,那么您必须执行一些额外的功能,如下所示:首先,确保您已在控制器中加载了 URL 帮助程序,即:

$this->load->helper("url");

you have to first create a helper named "my_helper.php" at "application/helpers" directory with this code:

您必须首先使用以下代码在“application/helpers”目录中创建一个名为“my_helper.php”的帮助程序:

if ( ! function_exists('asset_url()'))
{
  function asset_url() {

    return base_url().'application/assets/';

  }
}

Now, you have to load this helper into your controller as below:

现在,您必须将这个助手加载到您的控制器中,如下所示:

$this->load->helper("my_helper");

Now replace your .htaccess code at 'application/'' directory with the below code:

现在用以下代码替换“application/”目录中的 .htaccess 代码:

RewriteEngine on
RewriteCond  !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]

After doing all the above functionality, you have to declare your assets in view as below:

完成上述所有功能后,您必须在视图中声明您的资产,如下所示:

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

回答by Vivek Mandal

First open config.php file and give base_url like

首先打开 config.php 文件并给出 base_url 之类的

$config['base_url'] = 'http://localhost/your_file_name/';

after this load helper into your controllers file like this:

在此加载助手到您的控制器文件之后,如下所示:

$this->load->helper('url');

then goto your view folder and give link like this:

然后转到您的视图文件夹并提供如下链接:

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/stylesheet/main.css">