我一直在使用 Codeigniter
的文档,我希望有一个外部配置文件用于分页,所以我做了以下内容:
我将 $config
数组放在 config/pagination.php
然后在每个控制器上我需要使用配置文件进行我将使用的分页:
$this->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$pag_links = $this->pagination->create_links();
我以为就是这样 . 问题是它不起作用 . 我在网站上搜索了这个,发现这两个问题已经回答:How to change data in custom loaded config array和autoload config for pagination in codeigniter not working
他们都建议了以下代码行:
$this->pagination->initialize($config);
但是Codeigniter的文档说:
在配置文件中设置首选项如果您不想使用上述方法设置首选项,则可以将它们放入配置文件中 . 只需创建一个名为pagination.php的新文件,在该文件中添加$ config数组 . 然后将文件保存在application / config / pagination.php中,它将自动使用 . 如果将首选项保存在配置文件中,则不需要使用$ this-> pagination-> initialize() .
那么,重要的是 You will NOT need to use $this->pagination->initialize() if you save your preferences in a config file.
当然,如果我把这行代码放在我自己的代码中就行了 .
$this->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$pagination->initialize($config);
$pag_links = $this->pagination->create_links();
我错过了一些重要的东西吗? What's the right way to create an external configuration file for Codeigniter's pagination?
1 回答
您的配置数组应存储在application / config / pagination.php中,然后您不需要添加:
在控制器中只调用库
因为你创建的配置有这些...我只能想到你是否在配置文件中使用变量$ total_rows等自动加载,因为这些不会引用外部文件中的任何内容 . 你需要直接指定值,例如:
但是,如果需要在控制器内指定这些值,则需要使用您所说的方式并使用
在控制器中 .