我花了几个小时来制作一个简单的 anchor 链接,但没有成功。
我的 Controller 是
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('template');
}
public function contact()
{
$this->load->view('contact');
}
}
我的template.php
View 基本上是一个带有
<a class="menuhref"> <?php echo anchor('welcome/contact/','Contato')?> </a>
我在views目录中还有一个contact.php。
我的config.php
是
$config['base_url'] = 'localhost';
$config['index_page'] = 'index.php';
当 template.php
加载并单击链接“contato”时,我想将系统驱动到函数 contact()
,该函数将负责打开页面 contact.php
。但是,我遇到了以下错误:
"/localhost/index.php/welcome/contact" was not found on this server
缺少什么?
最佳答案
base_url
配置必须包含协议(protocol)以及尾部斜杠。
来自config.php
文件:
URL to your CodeIgniter root. Typically this will be your base URL,
WITH a trailing slash:http://example.com/
If this is not set then CodeIgniter will guess the protocol, domain
and path to your installation.
因此,您可以按如下方式设置base_url
,或者在这种情况下只需将其留空:
$config['base_url'] = 'http://localhost/';
# base_url should be absolute, If you've installed CI in sub-folder:
$config['base_url'] = 'http://localhost/path/to/codeigniter_folder/';
旁注:为了使用 URL 辅助函数,例如 anchor()
,首先加载辅助文件:$this->load->helper('url');
(或通过 autoload.php
配置文件自动加载助手)。
为什么base_url
配置会影响anchor()
函数?
anchor()
函数,uses site_url()
辅助函数来确定超链接的 URL 地址。
还有site_url()
本身,uses two base_url
and index_page
配置来创建 URL 地址。
因此,如果您为 base_url
和/或 index_page
配置分配了错误的值,anchor()
函数将无法正常工作。
关于php - CodeIgniter anchor 错误: URL not found on this server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21653550/