We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Extending Paginator Class

Hi,

Is it possible to extend \Phalcon\Paginator\Adapter\Model, so that I could add some feature on it? If yes, what is the better way to do this? I have following class in Library folder

<?php
namespace CustomShopping\Libraries\Paginator; // CustomShopping\Libraries is registered in bootstrap file

class Paginator extends \Phalcon\Paginator\Adapter\Model
{
  public function __construct(){
      parent::__construct();
  }

  public function customFunction(){
      return "Hello world";
  }
}
?>

And then in my controller i used it like

$paginator = new CustomShopping\Libraries\Paginator(
    array(
        'data'  => $users,
        'limit' => $limit,
        'page'  => $page,
    )
);

and in view i called it like,

$paginator->customFunction();

I get the error CustomShopping\Libraries\Paginator not found error. Thanks for the help



6.6k
Accepted
answer

Okay i have got it working myself

Pagination.php in library folder

use Phalcon\Paginator\Adapter\Model;

class Pagination extends \Phalcon\Paginator\Adapter\Model
{
    public function __construct(array $config){
        parent::__construct($config);
    }

    public function customFunction(){
        return "Hello world";
    }
}

and in controller i called it like

$pagination = new \Pagination();