php - CodeIgniter anchor 错误: URL not found on this server

标签 php codeigniter anchor codeigniter-2

我花了几个小时来制作一个简单的 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/

相关文章:

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - 如何判断ob_start();已经被调用

php - 我需要使用 mysql 比较不同行中的两个不同列

php - 为什么我的 .htaccess 在部署时停止工作

javascript - Twitter处理链接的方式: click then AJAX,打开一个新选项卡然后完全加载

javascript - 如何单击列表项内的 anchor 标记?

php - 如何从 PHP 的 html 页面中抓取 H2 和 H3 标签?

php - 访问 stdClass 对象中的属性

codeigniter - 使用 codeigniter 在 mvc 项目上使用 phpdoc

javascript - text 和 textContent 属性之间的区别