我使用了两种解决方案:
应用程序/控制器/one.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class One extends CI_Controller
{
public function index()
{
$this->load->helper('url'); // important!, check auto-load note
$this->load->view('one');
}
}
/* End of file one.php */
/* Location: ./application/controllers/one.php */
application/views/one.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>One</title>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen"/>
</head>
<body>
<!-- page content -->
</body>
</html>
为了自动加载helper:
如果你发现你需要一个特定的帮助者
应用程序,您可以告诉 CodeIgniter 在系统期间自动加载它
初始化。这是通过打开
application/config/autoload.php 文件并将帮助程序添加到
自动加载数组。
这样就不必在每个控制器中写入$this->load->helper('url');。
另一个解决方案是从您的主配置文件 (application/config/config.php) 中检索“base_url”项并避免加载 URL 帮助文件:
application/controllers/two.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Two extends CI_Controller
{
public function index()
{
$this->load->view('two');
}
}
/* End of file two.php */
/* Location: ./application/controllers/two.php */
application/views/two.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Two</title>
<!-- We retrieve the config item -->
<link rel="stylesheet" href="<?php echo $this->config->item('base_url'); ?>css/style.css" type="text/css" media="screen"/>
</head>
<body>
<!-- page content -->
</body>
</html>
希望对你有帮助