Jayesh Vekariya
Jayesh Vekariya

Reputation: 95

How to customise the route in CodeIgniter?

I am using CodeIgniter. For example, my website is test.com

My default controller is Home

Home controller code

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

}

When a URL like test.com is entered, it produce output form home_view, and it is ok.

When a URL like test.com/home/index/Michael/Clarke is entered, I get this output:

First Name = Michael
Last Name = Clarke

I want above out on URL like test.com/Michael/Clarke instead of test.com/home/index/Michael/Clarke.

How can I remove home/index?

Upvotes: 3

Views: 86

Answers (1)

Ariful Haque
Ariful Haque

Reputation: 3750

In your application\config\route.php

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

In your controllar

 public function index($first, $second){

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

    }//end function

will print for example.com/ARIFUL/haque as

first ARIFUL 
second haque

Upvotes: 2

Related Questions