1

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?

1 Answer 1

2

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');
}
Sign up to request clarification or add additional context in comments.

6 Comments

Nice, thank you :-). What is the difference of using public function index(*$id*) and $id = $this->uri->segment(2);?
No problem, Well you could still use segments, I just think that arguments are fancier :P
if you do public function index($id), you HAVE to pass in an id like this page/index/1. if you try to access page/index, it will not work.
@Catfish He can still make it optional: index($id=false)
I know, but he asked what the difference was between index($id) and the $this->uri->segment() so i was just trying to clarify.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.