使用Codeigniter的分页,不带索引功能 [英] Pagination with Codeigniter, without index function

查看:72
本文介绍了使用Codeigniter的分页,不带索引功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个控制器:

I have a controller for example:

class Users extends CI_Controller
{
    function index()
    {
        $this->load->library('pagination');

        $config['base_url'] = base_url() . 'index.php/users';
        $config['total_rows'] = 55;
        $config['per_page'] = 10;
        $this->pagination->initialize($config); 

        echo $this->pagination->create_links();
    }
}

然后分页将是:

base_url() . 'index.php/users/10
base_url() . 'index.php/users/20
base_url() . 'index.php/users/30
..

问题 strong>

它显示链接很好,但是当我单击它时显示404。
仅当链接像这样(具有该功能时,它才能正常工作)索引):

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
..

无论如何,我不会不想让用户看到带有 index 的地址,我该怎么办?
我想取消带有索引的地址+分页将不起作用。

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.

推荐答案

索引必须为否则,codeigniter不会知道要调用哪个函数(控制器中的 index()函数)。

The index has to be there otherwise codeigniter won't know which function to call (the "index()" function in your controller).

最好的选择是使用URI路由。

Your best bet is to use URI routing.

添加类似这样的内容(未经测试):

Add something like this (untested):

$route['users/page/:num'] = "users/index";

进入文件 application / config / routes.php

Into the file "application/config/routes.php"

但是,分页库可能仍会添加索引,在这种情况下,您需要扩展索引或创建自己的索引。但是,使用以下基本url可能会起作用:

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

未经测试的收益。抱歉,未经测试的示例,但是您一定会需要uri路由来完成所需的操作,有关uri路由的更多信息在此处

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

为了处理旧uri工作中出现的所有SEO问题,您可以尝试以下任何一种方法。

In order to deal with any SEO issues with the old uri working you can try any of the following.

要在用户每次点击您不希望出现的uri格式时进行重定向,可以将其放置在index()函数顶部,将其重定向到正确的版本。用户控制器。

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

或者,您可以在HTML中设置规范的元标记,这告诉搜索引擎蜘蛛说,尽管它所浏览的页面应该被视为规范标记中的页面,而不是地址栏。在此处

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.

您也可以通过htaccess进行此操作,但是在我厌倦尝试举任何示例的那一刻,却没有进行测试的方法。

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.

这篇关于使用Codeigniter的分页,不带索引功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆