Question

I have a controller named 'list'. It simply lists users after retrieving them from database. The view has pagination, but it is my own pagination. What I want to do is this:

www.example.com/list/<page_num> --> show records belongs to page number given
www.example.com/list --> show first page, i.e. it is equal to '...com/list/1'

To do so, I have a route like this:

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

The problem arises when I want to assign link to href's at page anchors. For example, when I set link for the second page like href="list/2", that 'list' word adds up to the uri and after some clicks I got something like:

www.example.com/list/list/list/3

If I use href="2", then link becomes

www.example.com/2

which is invalid. I can use "/" at the end of controller name, I mean instead of using '...com/list', I can use '...com/list/', however it is not the best solution I suppose since some users want to 'pretify' it deleting the slash at the end. I am enough of using base_url for every link, img, css, etc! Is there a solution to this problem?

Was it helpful?

Solution

I would make code simillar to this one

function index() {

    redirect('list/1'); //whatever default or you can try _remap()

}

function list( $page = FALSE ) {

    if ($page === FALSE || !$this->_validPageFunction( $page )) $page = '1';

    //... whatever needs to be done here

}

private function _validPageFunction( $page ) {

    // return FALSE (not-valid) or TRUE (valid)

}

sidenote: try not bother with routes that much, use index() function to redirect to default, routes can be/get messy

sidenote2: always use base_url(''); for links.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top