我正在尝试学习 Codeigniter。我按照 CI 站点上的教程构建了一个简单的 MVC。这是我的 Controller ...
<?php
class Pages extends CI_Controller {
public function view($page = 'home'){
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
这是标题模板...
<html>
<head>
<title><?php echo $title ?> - WurkWithUs</title>
<link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/bootstrap.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/custom.css" type="text/css" media="screen"/>
</head>
<body>
<h1>Wurk With Us</h1>
当我使用像上面那样的绝对路径时,样式表起作用。但是,如果我尝试使用类似...
<link rel="stylesheet" href="<?= base_url()?>css/bootstrap.css" type="text/css" media="screen"/>
我得到一个空白页。当我“查看页面源代码”时,我的样式行看起来像...
<link rel="stylesheet" href="
我已尝试遵循建议 here我得到了相同的结果,即文档在“href=”行停止被解析。
我做错了什么?有没有更好的方法来加载我的样式表?
最佳答案
这个问题我遇到过很多次了。我敢打赌你不会在 URL helper 中加载.它不会自动加载,因此要么将其加载到您的 Controller 中,要么自动加载。
如果你选择在你的 Controller 中引导它,调用
$this->load->helper('url');
在加载 View 之前。
您在源代码中看到 href
空白的原因是因为此时出现 PHP 错误(它找不到方法 base_url()
)
关于php - 样式表的 Codeigniter 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550148/