Cyclone
Cyclone

Reputation: 15269

Codeigniter index page

I want to create a page, which I will be able to browse like this: /page/5.

I have created a page.php controller and I can view this page, here is its content:

class Page extends CI_Controller {

public function __construct()
{
    parent::__construct();
}

public function index()
{
    $this->load->view('template/header');

    $id = $this->uri->segment(2);
    $this->load->view('template/middle', array('id' => $id));
    $this->load->view('template/footer');
}

}

The problem is that I cant reach the page number from the uri.

To be clear:

This is reachable: index.php/page.

But at this url, I'm getting the 404: index.php/page/5

I know I can make another controller function to view the page like this /index.php/page/id/5, but I want it be be accessable via the index function.

Is that possible?

Upvotes: 1

Views: 1395

Answers (1)

CodeOverload
CodeOverload

Reputation: 48505

CodeIgniter doesn't know whether you're looking for 5() or index(5)

To fix this, Try setting up a custom route in config/routes.php:

$route['page/(:num)'] = 'page/index/$1';

Also, In your index function:

public function index($id)
{
    $this->load->view('template/header');
    $this->load->view('template/middle', array('id' => $id));
    $this->load->view('template/footer');
}

Upvotes: 2

Related Questions