Pagination with Codeigniter, without index function
I have a controller for example:
class Users extends CI_Controller
{
function index()
{
$this->load->library('pagination');
$con开发者_如何学Pythonfig['base_url'] = base_url() . 'index.php/users';
$config['total_rows'] = 55;
$config['per_page'] = 10;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
Then the pagination will be:
base_url() . 'index.php/users/10
base_url() . 'index.php/users/20
base_url() . 'index.php/users/30
..
THE PROBLEM
It displays the links fine, but when I click it shows 404. It works fine only if the links will be like this (with the function index):
base_url() . 'index.php/users/index/10
base_url() . 'index.php/users/index/20
base_url() . 'index.php/users/index/30
..
Anyway, I don't want that the users will see the address with index
, what should I do?
I want to cancel the address with index + the pagination will work without it.
The index has to be there otherwise codeigniter won't know which function to call (the "index()" function in your controller).
Your best bet is to use URI routing.
Add something like this (untested):
$route['users/page/:num'] = "users/index";
Into the file "application/config/routes.php"
However the pagination library may still add the index in which case you would need to extend it or create your own. However using the following base url may work:
class Users extends CI_Controller
{
function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url() . 'index.php/users/page/';
$config['total_rows'] = 55;
$config['per_page'] = 10;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
Again that is untested. Sorry for the untested examples, but you will definitely need uri routing to do what you need, there is further information about uri routing here
In order to deal with any SEO issues with the old uri working you can try any of the following.
To redirect every time a user hits the uri format that you don't want to appear you can redirect to them to the correct version by putting this at the top of your index() function in the users controller.
$this->load->helper('url');
$current_uri = uri_string();
if(strpos($current_uri, 'users/index'))
{
$new_uri = str_replace('users/index','users',$current_uri);
redirect($new_uri);
}
Alternatively you can set a canonical meta tag in the HTML, this tells search engine spiders that although the page it's looking it at should be treated as the page in the canonical tag, not the address bar. Find more on the canonical tag here
<link rel="canonical" href="<?php echo site_url('users/'.$page_num); ?>" />
Of course you will have to adjust the variables as necessary above.
You could also do it via htaccess but without the means to test it just at the moment i'm weary of trying to give any examples.
try this:
$route['users'] = "users/index";
$route['users/([0-9]+)] = "users/index/$1";
Maybe you could try rewriting the url in a .htaccess ?
I don't remember the exact syntax, though...
精彩评论