【问题标题】:How to include URL suffix in pagination links in CI?如何在 CI 的分页链接中包含 URL 后缀?
【发布时间】:2013-10-17 02:14:42
【问题描述】:

我可以通过在 CI config.php 中添加后缀“.html”来将页面 example.com/index.php/products/view/shoes 作为 example.com/index.php/products/view/shoes.html文件。 [参考:http://ellislab.com/codeigniter/user-guide/general/urls.html]

但是当我在分页中生成链接时,后缀不会添加到生成的指向其他页面的链接中。

有没有可以通过配置进行设置或者需要修改库文件。

【问题讨论】:

  • 你所说的“后缀”是什么意思?你能在这里发布一些例子或代码吗?
  • 更新了更多细节。

标签: codeigniter pagination


【解决方案1】:

你可以这样定义分页链接的后缀:

$config['suffix'] = YOUR_SUFFIX

看我的例子:

网址:http://www.test_store.com/products/index/order/low_price

// Parsing the URI
$uri = $this->uri->uri_to_assoc(3);

$page     = !empty($uri['page'])   ? $uri['page']   : 1;
$order    = !empty($uri['order'])  ? $uri['order']  : false;

// Search paginated
$this->Product->limit  = $per_page;
$this->Product->offset = ($page - 1) * $per_page;
$this->Product->order  = $order;

$products = $this->Product->get_all();

// Pagination config
$config['base_url']         = site_url('products/index/page');
$config['total_rows']       = $this->Product->count_all();
$config['uri_segment']      = 4;
$config['num_links']        = 5;
$config['use_page_numbers'] = TRUE;
$config['per_page']         = $per_page; 

// Add a suffix to pagination links
$config['suffix'] = !empty($uri['order']) ? '/order/'. $uri['order'] : ''; 

$this->pagination->initialize($config); 

$pagination = $this->pagination->create_links();

PD:我是阿根廷人,我的英语水平有点差

【讨论】:

  • 如果我通过带有扩展名的基本 url 则它不起作用,否则工作正常。但是对于第一页,当我没有在 base_url 中传递它时,我没有得到后缀。我该怎么做?
  • 你可以这样附加扩展名:$config['suffix'] = '.html';
  • 它适用于页面,但不适用于第一页。第一页只获取 base_url。
【解决方案2】:

在您的应用程序/config/config.php 中:

/*
|--------------------------------------------------------------------------
| URL suffix
|--------------------------------------------------------------------------
|
| This option allows you to add a suffix to all URLs generated by CodeIgniter.
| For more information please see the user guide:
|
| http://codeigniter.com/user_guide/general/urls.html
*/

$config['url_suffix'] = '';

【讨论】:

  • 正如我在问题中解释的那样,这适用于使用 anchor() 函数生成的链接,但不适用于分页中的链接。
  • 发布您的分页代码。对于分页,您需要手动创建它们。
  • 在分页中,我传递了不带后缀的 base_url,否则它会生成类似 www.example.com/controller/method.html/page/1 的代码,这些代码将不起作用。我想生成像 www.example.com/controller/method/page/1.html 这样的代码,但我在分页中找不到任何后缀参数来仅传递扩展 base_url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 2012-12-01
  • 2011-09-23
  • 1970-01-01
相关资源
最近更新 更多