php 如何在codeigniter ci中加载javascript css文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12119111/
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
How to load javascript css files in codeigniter ci
提问by newBike
i wanna migrate my website into CI.
i just simply modified from ci sample file welcome.php
in index() function , i load the view to show.
however , i put many javascripts and css files in the header file .
and call it by $this->load->view('header');but i can not load the javascript files correctly!
Can anyone give me some tips ? it;s hard to configure how to set the correct path.
我想将我的网站迁移到 CI。我只是简单地在 index() 函数中从 ci 示例文件welcome.php 修改,我加载了要显示的视图。但是,我在头文件中放置了许多 javascripts 和 css 文件。并调用它,$this->load->view('header');但我无法正确加载 javascript 文件!谁能给我一些提示?很难配置如何设置正确的路径。
<script type="text/javascript" src="/assets/javascripts/order.js"></script>
<script type="text/javascript" src="../assets/javascripts/order.js"></script>
<script type="text/javascript" src="../../assets/javascripts/order.js"></script>
my controller code as following
我的控制器代码如下
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->base = $this->config->item('base_url');
$this->load->view('header');
$this->load->view('welcome_message');
}
}
}
belows are my folder structure
下面是我的文件夹结构


采纳答案by jogesh_pi
put your assets folder with applications, system, assets
把你的资产文件夹放在 applications, system, assets
not in application and simple load the urlhelper class in controller where you call the header view part something like
不在应用程序中,url只需在控制器中加载助手类,您就可以在其中调用标题视图部分,例如
$this->load->helper('url');
$this->load->view('header');
and simply use something like this in your header file..
because $this->base_url()return the /folder..
只需在头文件中使用类似的内容..
因为$this->base_url()返回/文件夹..
<script src="<?php echo $this->base_url();?>assets/javascript/jquery.js"></script>
Changing the folder structure because access within the application folder is just for the core part that i know..
更改文件夹结构,因为应用程序文件夹内的访问仅适用于我所知道的核心部分。
here is the link if you want to know more about URL Helper
如果您想了解有关URL Helper 的更多信息,请点击此处的链接
回答by Rejoanul Alam
This is the best way with minimal code
这是使用最少代码的最佳方式
<script src="<?php echo base_url('assets/javascript/jquery.js');?>"></script>
<script src="<?php echo base_url('assets/css/bootstrap.min.js');?>"></script>
回答by Ernesto Codeigniter
Jogesh_p's answer will surely solve the assets loading problem you have. I would like to follow up on this question you gave
Jogesh_p 的回答肯定会解决您遇到的资产加载问题。我想跟进你提出的这个问题
Thank you or your support. btw the way if i wanna use some library like phpMailler or zend framwork .
谢谢您或您的支持。顺便说一句,如果我想使用像 phpMailler 或 zend framwork 这样的库。
You can put in application/libraries/
您可以放入应用程序/库/
Then load it in the controller using the Library's Class' Name
然后使用库的类名称将其加载到控制器中
$this->load->library('phpmailer');
Its your choice to load in on the constructor or on the individual method.
您可以选择在构造函数或单个方法上加载。
Good Luck!
祝你好运!
回答by Vishal Kumar Sahu
To solve my problem I created helper functions to load assets. And here is my code deployed in my application. PS: First I planned a good/flexible directory structure
为了解决我的问题,我创建了辅助函数来加载资产。这是我的应用程序中部署的代码。 PS:首先我规划了一个好的/灵活的目录结构
[some_helper.php]
[ some_helper.php]
/*
* Created: 2017/12/14 00:28:30
* Updated: 2017/12/14 00:28:39
* @params
* $url_structure = 'assets/js/%s.js'
* $files = ['main.min', 'social']
* $echo = FALSE
*
*/
function load_js($files = [], $url_structure = NULL, $version = '1.0', $echo = FALSE){
$html = "";
foreach ($files as $file) {
if($url_structure){
$file = sprintf($url_structure, $file);
}
$file_url = base_url($file);
$html .= "<script src=\"{$file_url}?v={$version}\"></script>";
}
if($echo) {
echo $html;
}
return $html;
}
/*
* Created: 2017/12/14 00:28:48
* Updated: 2017/12/14 00:28:51
* @params
* $version = '1.0' // Later load from configuration
* $url_structure = 'assets/js/%s.css'
* $files = ['main.min', 'social']
* $echo = FALSE
*
*/
function load_css($files = [], $url_structure = NULL, $version = '1.0', $echo = FALSE){
$html = "";
foreach ($files as $file) {
if($url_structure){
$file = sprintf($url_structure, $file);
}
$file_url = base_url($file);
echo "<link rel=\"stylesheet\" href=\"{$file_url}?v={$version}\">";
}
if($echo) {
echo $html;
}
return $html;
}
Then called in the view
然后在视图中调用
[some_view.php]
[ some_view.php]
$css = [
'path' => 'assets/css/%s.css',
'files' => ['bootstrap.min','style']
];
load_css($css['files'], $css['path'], '1.0', TRUE);
Hope it helps someone.
In the case of OP $css['path'] = 'application/assets/css/%s.css';will do the trick.
希望它可以帮助某人。在 OP 的情况下可以解决问题$css['path'] = 'application/assets/css/%s.css';。
Updated the code on Githubwhich I will keep updating.
更新了Github上的代码,我会继续更新。
回答by user3437895
assets/css/bootstrap.min.css"
资产/css/bootstrap.min.css”
<!-- Include JS -->
<script src="<?php echo base_url();?>assets/js/jquery.js"></script>
<script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
回答by user3437895
function addcategory()
{
//alert("<?php echo base_url();?>")
$.ajax({
complete: function() {}, //Hide spinner
url : '<?php echo base_url();?>category/search',
data:$('#add_category').serialize(),
type : "POST",
dataType : 'json',
success : function(data) {
if(data.code == "200")
{
alert("category added successfully");
}
},
beforeSend: function(XMLHttpRequest){}, //$.blockUI();
cache: false,
error : function(data) {
}
});
}

