Codeigniter url路由选择菜单项将段添加到url



在新安装的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";

我已经'消除' index.php从URL与。htaccess和config.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/"。然后我点击"Home"锚菜单项,浏览器中的url变成了localhost/test/home。我点击另一个菜单项"主页功能",主页功能页面加载,url变成localhost/test/home/homefunction。在home函数视图的菜单中,我单击"home"项,url变为localhost/test/home/home我期望成为 localhost/test/home。然后我点击菜单项'Home Function', Home Function视图没有加载,url变成了localhost/test/home/home/homefunction, Home Function页面是不是加载,我再次点击Home,我得到了url localhost/test/home/home/home,每次我点击Home Function菜单项,然后Home菜单项一个Home部分被添加到url中。

我知道这是一些简单的东西,可能与路由有关,但我卡住了,我在谷歌上找不到类似的问题。有人能帮忙吗?

加载url helper类。这会使你变得轻松而有活力。

$this->load->helper('url');
config/autolload .php
$autoload['helper'] = array('url');

和每次分配url为:

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

鼓励您在任何需要生成本地URL的时候使用此函数,以便在URL更改时您的页面变得更易于移植。

URL helper: codeigniter

选择

<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>

最新更新