【发布时间】:2019-12-29 21:57:16
【问题描述】:
我正在使用 Codeigniter 3.1.8 和 AngularJS v1.7.8 开发一个博客应用程序。
应用程序的仪表板是“纯”Codeigniter,带有模型、控制器和视图,而前端由 AngularJS 管理和显示的 JSON 组成。 (我在后端和前端之间操作了这种清晰的分隔,以启用“主题化”。)
帖子是分页的。在后端,在 Posts 控制器 中,帖子列表如下所示:
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;
$config['query_string_segment'] = $query_string_segment;
$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pagination'] = $this->pagination->create_links();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
// All posts
$this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}
在前端,在 AngularJS 中,我有:
// All posts
.controller('PostsController', ['$scope', '$http', function($scope, $http){
//Get current page (?page=2, ?page=3 etc)
const currPage = window.location.search;
// Get all the posts on the current page
$http.get('api/' + currPage).then(function(response) {
//Categories
$scope.categories = response.data.categories;
// Posts
$scope.posts = response.data.posts;
// posts pagination
$scope.pagination = response.data.pagination;
});
}])
问题是当应用程序不在在根目录(它位于 www.examplesite.com/blog,不在 www.examplesite.com)。
我想它与 Codeigniter 的 base_url() 有关系,但我无法发现错误并修复它。
我该如何解决这个问题?
【问题讨论】:
-
您能否详细说明在您的情况下“不起作用”是什么意思?
-
@LajosArpad 是的,看看 here。
-
那么,如果第 2 页指向 code-love.tk/apiblog/?page=2 而不是 code-love.tk/?page=2(等等),那么它会按照您的预期工作吗?
-
@LajosArpad 是的
标签: php angularjs codeigniter