0

在全新安装的 codeigniter 2.1.3 中,我有一个具有两个功能的控制器“home.php”:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()
    {
        $this->load->view('home');
    }

    public function homefunction()
    {
        $this->load->view('homefunction');
    }

}

我有一个视图'home.php'。锚点代表菜单:

<p>Home</p>
<br/>
<a href="home">Home</a>
<a href="home/homefunction">Home Function</a>

和一个视图'homefunction.php'

<p>Function in the Home controller</p>
<br/>
<a href="home">Home</a>
<a href="home/homefunction">Home Function</a>

我的路线:

$route['default_controller'] = "home";

我已经用 .htaccess 和 config.php 从 URL 中“消除”了 index.php,我的 .htaccess 是:

RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [PT,L]  

在我设置的 config.php 文件中:

$config['base_url'] = 'http://localhost/test';
$config['index_page'] = '';

现在我运行http://localhost/test并且我的主页加载了菜单。浏览器中的网址是localhost/test/. 然后我单击浏览器中的 url 变为的“主页”锚菜单项localhost/test/home。我单击主页功能页面加载的另一个菜单项“主页功能”,网址变为localhost/test/home/homefunction. 在主页功能视图的菜单中,单击“主页”项,网址变为localhost/test/home/home. 我期待成为 localhost/test/home. 然后我单击菜单项“主页功能”,主页功能视图未加载并且 url 变为localhost/test/home/home/homefunction,主页功能页面加载我再次单击主页并进入 url localhost/test/home/home/home,每次单击主页功能菜单项时都会继续然后主页菜单项在 url 中添加主页部分。

我知道这很简单,可能与路由有关,但我卡住了,我在谷歌上找不到类似的问题。任何人都可以帮忙吗?

4

2 回答 2

1

加载 url 帮助程序类。这将使您轻松而充满活力。

$this->load->helper('url');

或者默认加载它,在config/autoload.php

$autoload['helper'] = array('url');

每次您将网址分配为:

<a href="<?=site_url('home');?>">Home</a>
<a href="<?=site_url('home/homefunction');?>">Home Function</a>

建议您在需要生成本地 URL 的任何时候使用此功能,以便在您的 URL 更改时您的页面变得更加便携。

URL 助手:codeigniter

于 2013-01-02T11:24:54.333 回答
0

去做

<a href="/home">Home</a>
<a href="/home/homefunction">Home Function</a>

或者

<a href="<?=base_url('home');?>">Home</a>
<a href="<?=base_url('home/homefunction');?>">Home Function</a>
于 2013-01-02T11:13:25.757 回答