I have defined a function in which I use the pager, used in my controllers it works correctly. However, when I define a service to not copy and paste the code, I have the following:
app.paginacion:
class: Caja\SiafcaInternetBundle\Services\Paginacion
public: true
arguments: ["@knp_paginator"]
In my config.yml
knp_paginator:
page_range: "%knp_paginator_page_range%"
default_options:
page_name: "%knp_paginator_page_name%"
sort_field_name: "%knp_paginator_sort_field_name%"
sort_direction_name: "%knp_paginator_sort_direction_name%"
distinct: "%knp_paginator_distinct%"
template:
pagination: "%knp_paginator_pagination%"
sortable: "%knp_paginator_sortable%"
And in my controller I define the following class
class Paginacion{
private $paginator;
public function __contruct($paginator){
$this->$paginator = $paginator;
}
public function obtenerPaginacion($request,$aportantesWS,$totalAportantes)
{
if (!$aportantesWS)
{
$pagination = null;
}
else
{
// $paginator = $this->get('app.knp_paginator');
$pagination =
$this->paginator->paginate(
$aportantesWS,
$request->query->getInt('page', 1),
20
);
$pagination->setTotalItemCount($totalAportantes);
}
return $pagination;
}}
For example calling it:
$this->get('app.paginacion')->obtenerPaginacion($request,$aportantesWS,$totalAportantes)
When I try to use it, it generates the following error Error: Call to a member function paginate() on null Checking with dump() the request data , contributorsWS and totalcontributors come back non-null, that is, with data.
I found the solution. Following the following post on stackoverflow
Modify my definition in services.yml
Enter the calls -[setPaginator, ["@knp_paginator"]]
Modify my controller by adding the setPaginator method
Staying as follows: