php - Codeigniter 和 AngularJS 错误 : pagination does not work when the application is not in the root directory

标签 php angularjs codeigniter

我正在使用 Codeigniter 3.1.8AngularJS v1.7.8 开发一个博客应用程序

应用程序的仪表板是“纯”Codeigniter,带有模型、 Controller 和 View ,而前端由 AngularJS 管理和显示的 JSON 组成。 (我在后端和前端之间进行了这种清晰的分离,以实现“主题化”。)

帖子已分页。在后端的帖子 Controller 中,帖子列表如下所示:

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() 有关,但我无法发现错误并修复它。

如何解决这个问题?

最佳答案

看一下这一行:

$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());

这将路径指定为 "/",该路径作为 $path 传递,因此在您的 function 中,链接路径将为

$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . $path;

这只会在根 URL 的末尾添加一个/。当不在根目录上时,您需要在索引处传递正确的后缀,而不是 "/"

关于php - Codeigniter 和 AngularJS 错误 : pagination does not work when the application is not in the root directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523652/

相关文章:

php - 我的 PHP PDO 不会运行对数据库 PHPMYADMIN 的更新查询

php - 隐藏 jQueryUI 选项卡内容

javascript - 如何在 Angular JS 中进行变量路由

php - 使用javascript将输入文件的图像发布到php文件

php - codeigniter 2.02 - 传递参数 - 找不到页面

php - CodeIgniter-Inner Join 5个或更多表(SQL)

javascript - 根据动态选择选项值更改文本框值

php - 使用 MySQL 数据库中的多个选项预填充下拉菜单

javascript - 取消选中 angularjs 中的复选框

angularjs - Angular 绑定(bind)在数据属性中不起作用