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

时间:2013-01-02 11:10:30

标签: codeigniter url routing routes

在一个全新的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/。然后我点击“主页”主播菜单项,浏览器中的网址变为localhost/test/home。我点击其他菜单项“Home Function”加载主页功能页面,网址变为localhost/test/home/homefunction。在主页功能视图的菜单中,我点击“主页”项,网址变为localhost/test/home/home我原本希望成为 localhost/test/home。然后我单击菜单项'Home Function',主页功能视图未加载,网址变为localhost/test/home/home/homefunction,主页功能页面加载我再次点击主页,我进入网址localhost/test/home/home/home,每当我点击主页功能菜单项,然后主页菜单项在主页中添加主页部分时,这会继续。

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

2 个答案:

答案 0 :(得分:1)

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

$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 helper: codeigniter

答案 1 :(得分: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>