Welcome Guest, Not a member yet? Register   Sign In
How can I change the structure of the links that create_links() generates
#1

I am working on a small blogging application. There is a clear separation between its back-end and its front-end:

  1. The back-end is an [b]API[/b], made with Codeigniter 3, that spits out pages, posts, pagination etc.

  2. This API is consumed by an [b]AngularJS[/b] (v. 1.7.x) front-end;
The [b]front-end posts controller[/b] looks like this:
Code:
// 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) {

        // Posts
        $scope.posts = response.data.posts;

        // posts pagination
        $scope.pagination = response.data.pagination;
    });
}])
I have a problem with the pagination that Codeigniter (API) generates caused by the fact that its root is 

Code:
http://apiblog.com/api/
. The pagination's HTML is this:
Code:
<ul class="pagination">
    <li><a href="http://apiblog.com/api/" data-ci-pagination-page="1" rel="prev">&lsaquo;</a></li>
    <li><a href="http://apiblog.com/api/" data-ci-pagination-page="1" rel="start">1</a></li>
    <li class="active"><span>2</span></li>
    <li><a href="http://apiblog.com/api/?page=3" data-ci-pagination-page="3">3</a></li>
    <li><a href="http://apiblog.com/api/?page=4" data-ci-pagination-page="4">4</a></li>
    <li><a href="http://apiblog.com/api/?page=3" data-ci-pagination-page="3" rel="next">&rsaquo;</a></li>
</ul>
By looking at the front-end posts controller, you can see that the pagination links should [b]miss[/b] the api/ part. It [b]should[/b] be:
Code:
<ul class="pagination">
    <li><a href="http://apiblog.com/" data-ci-pagination-page="1" rel="prev">&lsaquo;</a></li>
    <li><a href="http://apiblog.com/" data-ci-pagination-page="1" rel="start">1</a></li>
    <li class="active"><span>2</span></li>
    <li><a href="http://apiblog.com/?page=3" data-ci-pagination-page="3">3</a></li>
    <li><a href="http://apiblog.com/?page=4" data-ci-pagination-page="4">4</a></li>
    <li><a href="http://apiblog.com/?page=3" data-ci-pagination-page="3" rel="next">&rsaquo;</a></li>
</ul>
Given that the pagination items are generated "behind the scenes" by 

Code:
$data['pagination'] = $this->pagination->create_links();


, I was unable to achieve the necessary configuration of the pagination links.
How can I achieve this?
Reply
#2

Did you try setting the base url in the pagination config?

PHP Code:
$this->load->library('pagination');

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;

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

echo 
$this->pagination->create_links(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-05-2019, 03:15 AM)InsiteFX Wrote: Did you try setting the base url in the pagination config?

PHP Code:
$this->load->library('pagination');

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;

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

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


In api/application/config/config.php, I have this "general purpose" base_url that I need to keep:

PHP Code:
$root  "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root

This base_url is dynamic, and it needs to stay so (the same is true for any other base_url) because this is intended as a "general purpose" CMS.

Can you tell me a method for configuring the base_url only for the front-end posts, as needed, dynamically?

Thanks!
Reply
#4

You can look in the url helper it will show you how CodeIgniter is doing it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB