我如何在codeigniter中包含css和js文件


How can I include css and js file in codeigniter

我试图在下面的代码中包含css文件。

配置:

$config['base_url'] = 'http://localhost/ASOFT/Projects/CI_search';
$config['site_url'] = 'http://localhost/ASOFT/Projects/CI_search/index.php';
$config['js'] = 'assets/js';

视图:

<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/style.css">
<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/bootstrap.min.css">
<script src="<?php echo $base?>/<?php echo $js?>/jquery.min.js"></script>
<script src="<?php echo $base?>/<?php echo $js?>/jquery.js"></script>
<script src="<?php echo $base?>/<?php echo $js?>/bootstrap.min.js"></script>

我把css文件放在CI_search/css中,js文件放在CI_search/assets/js中我得到了未定义变量css和未定义变量js的错误。请提供解决方案。


控制器:

public function index() {     
    $this->data = array(
         'site' => $this->config->item('site_url'), 
         'base' => $this->config->item('base_url'), 
         'css' => $this->config->item('css'), 
         'js' => $this->config->item('js'), 
         'image'=>$this->config->item('image') 
    ); 
   $data = $this->data; 
   $data['error'] = ''; 
   $this->load->view('index',$data); 
}

据我所知,你没有在你的配置文件中声明$config['css'],所以得到css的未定义变量错误是正常的。但是使用js应该不会有问题。

声明base_url时,在末尾使用斜杠(例如:"http://localhost/fancysite/")

还可以使用CI的url帮助器来使用base_url()或site_url()等函数。(就像@Likee建议的那样)。

config。

// this should be the first variable in CI's config.php
$config['base_url'] = "http://localhost/ASOFT/Projects/CI_search/";
// many more lines with other configuration variables
// ..................................................
// ..................................................
// ..................................................
// your own configuration variables at the end of file
$config['css'] = 'css/';
$config['js'] = 'assets/css/';
$config['image'] = 'images/';
控制器

public function index() {
    // loading url helper to use base_url() function in the view,
    // if you load this helper in autoload.php you don't need to load it here again
    $this->load->helper('url');
    // if you didn't declare data as class property
    // you can simply use
    // $data = array(
    //         'css' => $this->config->item('css'),
    //         'js' => $this->config->item('js'),
    //         'image'=>$this->config->item('image')
    //          );
    $this->data = array( 
         'css' => $this->config->item('css'), 
         'js' => $this->config->item('js'), 
         'image'=>$this->config->item('image') 
    ); 
    // you really don't need the line below
    // $data = $this->data;
    $this->data['error'] = ''; 
    $this->load->view('index',$this->data); 
}
<<p> 视图/strong>
<link rel="stylesheet" href="<?php echo base_url($css . 'style.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url($css . 'bootstrap.min.css'); ?>">
<script src="<?php echo base_url($js . 'jquery.min.js'); ?>"></script>
<!-- do you really need to include jquery.js while you included jquery.min.js above? but here we go :) -->
<script src="<?php echo base_url($js . 'jquery.js'); ?>"></script>
<script src="<?php echo base_url($js . 'bootstrap.min.js'); ?>"></script>

可能你需要使用url helper很多。所以你可以自动加载到配置目录下的autolload .php文件中。一定在第90行

$autoload['helper'] = array('url');

在codeigniter中包含css和js文件的方法有很多。我就是这样做的。

首先:在应用程序文件夹外创建一个文件夹,并将其命名为任何你喜欢的名称,但我更喜欢将其命名为资源。创建一个子文件夹,如js、css、images等。把你所有的文件放在这里,以便将来参考。

第二:打开保存在application/config中的contents .php文件,粘贴以下代码。

define('CSS',       'http://localhost/yourfolder/resources/css');
define('JS',        'http://localhost/yourfolder/resources/js');
define('IMAGES',    'http://localhost/yourfolder/resources/images');

注意:更新此代码取决于您的需要。

第三:在你的视图文件中,你可以通过

访问这些文件
<link rel="stylesheet" href="<?php echo(CSS.'bootstrap.min.css'); ?>">
<script type="text/javascript" src='<?php echo(CSS.'bootstrap.min.css'); ?>'></script>

参考这个链接。如何在CodeIgniter中包含CSS文件?

希望这对你有帮助。

像这样将变量绑定到view:

$this->load->view('viewName', $config);

array $config应该在你的控制器方法中可用。

可以使用function base_url()代替$config['base_url']。

如果可能的话,确定你的路径结构是什么,然后在视图中使用base_url()。

<link href="<?php echo base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">
<script src="<?php echo base_url();?>assets/js/blahblah.js"></script>

你甚至可以有几个简单的模板,然后调用适当的模板。

如果这是不可能的-然后将这个逻辑和输出推到一个模型或类似的,以便它可以被不同的控制器调用。否则你将不得不更新控制器中的文件夹名称和文件路径。