2012-11-07 41 views
0

我真的很難理解CI中的URL/URI路由。在這種情況下,我有兩個鏈接,一個是Home,另一個是Panel,Home鏈接到主/面板的主/索引和麪板鏈接,以及更好理解的片段。php:CI中的路由(Codeigniter)

<a href="main/index"> Home </a> 
<a href="main/panel"> Panel </a> 

,這是控制器main.php

class Main extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
     $this->load->view('templates/header'); 
     $this->load->view('home'); 
     $this->load->view('templates/footer'); 
    } 

    public function panel() 
    { 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
     $this->load->view('templates/header'); 
     $this->load->view('panel'); 
     $this->load->view('templates/footer'); 
    } 
} 

和我的繼承人routes (config/routes.php)

$route['main/index'] = "main/index"; 
$route['main/panel'] = "main/panel"; 
$route['default_controller'] = "main/index"; 

在第一次運行時,它會自動進入到主/指數的代碼,它工作正常,但是當我點擊面板鏈接時,它說對象未找到 Home鏈接也是如此對象沒有發現

回答

1

首先,你最好有相對路徑的根在HREF:

<a href="/main/index"> Home </a> 
    <a href="/main/panel"> Panel </a> 

,或者甚至更好這樣的:

<a href="<?=$base_url;?>main/index"> Home </a> 
    <a href="<?=$base_url;?>main/panel"> Panel </a> 

接下來的事情是視圖,您正在加載,正確的方法是加載控制器功能中的一個視圖:

$this->load->view('home'); 

and in home.php you need包括您的其他意見,home.php:

<?php $this->load->view('templates/header');?> 
    ... 
    <!--YOUR HOME HTML GOES HERE--> 
    ... 
    <?php $this->load->view('templates/footer');?> 

現在的路由。確保使用/index.php/[controller]/[function]鏈接(除非你使用的是像URL重寫這裏http://ellislab.com/codeigniter/forums/viewthread/180566/

路由配置:

$route['default_controller'] = "main/index"; //this is the only thing you need to define 

所有網頁後,將通過訪問此類URL:

索引頁:http://example.com/http://example.com/index.php/main/index

面板頁:http://example.com/index.php/main/panel

+0

是是試圖重寫它的工作原理,但問題是這個http:// localhost/projects /庫/主/主/主/主/主/主/主/面板我只點擊面板鏈接多次,但鏈接似乎堆疊每次點擊我做 – lemoncodes

+0

注意到我提到的第一件事 - 你應該有適當的鏈接 – Serg

+0

你的意思是即使我已經刪除了index.php我必須包括基本的網址? – lemoncodes