我有这个Paginator
类构造函数:
class Paginator
{
public function __construct($total_count, $per_page, $current_page)
{
}
}
Paginator
服务已在Ibw/JobeetBundle/Resources/config/services.yml
中注册,如下所示:
parameters:
ibw_jobeet_paginator.class: Ibw\JobeetBundle\Utils\Paginator
services:
ibw_jobeet_paginator:
class: %ibw_jobeet_paginator.class%
当我像这样使用Paginator
时:
$em = $this->getDoctrine()->getManager();
$total_jobs = $em->getRepository('IbwJobeetBundle:Job')->getJobsCount($id);
$per_page = $this->container->getParameter('max_jobs_on_category');
$current_page = $page;
$paginator = $this->get('ibw_jobeet_paginator')->call($total_jobs, $per_page, $current_page);
我得到了这个例外:
警告:缺少参数1 Ibw \ JobeetBundle \ Utils \ Paginator :: __ construct(),调用 /var/www/jobeet/app/cache/dev/appDevDebugProjectContainer.php上线 1306并定义于 /var/www/jobeet/src/Ibw/JobeetBundle/Utils/Paginator.php第13行
我认为将参数传递给Paginator
服务构造函数有问题。你能告诉我,如何将参数传递给服务构造函数?
答案 0 :(得分:23)
那么,要回答您的问题,请使用arguments参数传递服务构造函数参数:
services:
ibw_jobeet_paginator:
class: %ibw_jobeet_paginator.class%
arguments:
- 1 # total
- 2 # per page
- 3 # current page
当然,由于参数是动态的,因此并没有真正帮助你。
相反,将参数从构造函数移动到另一个方法:
class Paginator
{
public function __construct() {}
public function init($total_count, $per_page, $current_page)
{
}
}
$paginator = $this->get('ibw_jobeet_paginator')->init($total_jobs, $per_page, $current_page);