Raz*_*fir 5 php codeigniter angularjs
我正在使用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));
}
Run Code Online (Sandbox Code Playgroud)
在前端,在 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;
});
}])
Run Code Online (Sandbox Code Playgroud)
问题是当应用程序不在根目录中(它位于 www.examplesite.com/blog,而不是 www.examplesite.com)时,分页不起作用(链接指向错误的链接)。
我想它与 Codeigniter 有关系,base_url()但我无法发现错误并修复它。
我该如何解决这个问题?
看看这一行:
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
Run Code Online (Sandbox Code Playgroud)
这指定了路径 as "/",它被传递为$path,所以在你function的链接路径将是
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;
Run Code Online (Sandbox Code Playgroud)
它只会在根 URL 的末尾放置一个 / 。您需要在 index 处传递正确的后缀,而不是"/"在不在 root上时传递。
小智 1
问题就在这里:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;
Run Code Online (Sandbox Code Playgroud)
因为$_SERVER['HTTP_HOST']始终是根目录,没有其他目录。
你需要使用类似的东西:
$config['base_url'] = str_replace("/api/", "/", base_url()) . $path;
Run Code Online (Sandbox Code Playgroud)
但我建议你在前端(Angular)中实现分页,而在 CI 中你只需要类似的东西:
public function index($offset, $limit) {
$data = [];
$data['count'] = $this->Posts_model->get_num_rows();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->get_posts($limit, $offset);
$this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
489 次 |
| 最近记录: |