簡體   English   中英

如何在CodeIgniter中自定義路由?

[英]How to customise the route in CodeIgniter?

我正在使用CodeIgniter。 例如,我的網站是test.com

我的默認控制器是Home

Home控制器代碼

class Home extends CI_Controller
{
    public function index($firstname = NULL, $lastname = NULL)
    {
        if(!$firstname && !$lastname)
        {
            $this->load->view('home_view');
        }
        else
        {
            echo "First Name = " . $firstname . "<br>";
            echo "Last Name = " . $lastname ;
        }   
    }

}

當輸入類似test.com的URL時,它會生成home_view輸出,並且沒問題。

當輸入像test.com/home/index/Michael/Clarke這樣的URL時,我得到這個輸出:

First Name = Michael
Last Name = Clarke

我想在上面找到像test.com/Michael/Clarke這樣的網址而不是test.com/home/index/Michael/Clarke

如何刪除home/index

在您的application\\config\\route.php

//UPDATE
// to achive example.com/firstname/lastname, use this route.
$route['(:any)/(:any)']                  = "test/index/$1/$2";

在你的控制器中

 public function index($first, $second){

        echo "first ".$first."<br>";
        echo "second ".$second;

    }//end function

將打印為example.com/ARIFUL/haque as

first ARIFUL 
second haque

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM