this->get('foobar') 给出了 "Call to a member function get() on a non-object" Symfony2



这是我第一次在这里发帖,所以任何改进我的发帖的提示/建议总是不胜感激。(我不打算把这作为我的最后一篇文章:])

我将控制器定义为服务,因此我可以在需要时从控制器内的其他控制器调用方法。到目前为止,一切都很顺利,但突然之间,我收到"在非对象上调用成员函数 get()"错误。我完全不知道它可能是什么,因为它在多个其他场合工作。

我现在只使用 Symfony2 6 周了,所以当你看到它时,请原谅写得不好的代码。

这是生成错误的方法:

public function generate1PDF($customer)
{
    if (file_exists('/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf')) {
        //PDF already exists. Delete it (because generating a new one will have more recent data)
        unlink('/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf');
    }
     //THIS IS THE LINE THAT THROWS THE EXCEPTION
    $arr = $this->get('itr.login.report.controller')->prepareReport($customer);
    if ($arr instanceof Response) {
        return $arr;
    }
    $data = $arr['data'];
    $report = $arr['report'];
    $this->get('knp_snappy.pdf')->generateFromHtml(
        $this->renderView(
            'itrLoginBundle:Report:report.html.twig',
            array(
                'report' => $data,
                'klant' => $customer,
                'rapport' => $report,
                'cmd' => false
            )
        ),
        '/pdf/' . $customer->getGoogleViewID() . '/' . date("Y") . '/' . date("Y-m") . '.pdf'
    );
    $this->reportRepository->updateGenerated($report);
}

这是引发异常的行应导航到的方法:

public function prepareReport($customer, $report = null)
{
    if ($report == null) {
        $reportUitDB = $this->reportRepository->findLatestReportByCustomerId($customer->getId());
    } else {
        $reportUitDB = $report;
    }
    if ($reportUitDB == null) {
        return null;
    } else {
        $data = json_decode($reportUitDB->getData(), 'json');
        return array('data' => $data, 'report' => $reportUitDB);
    }
}

这是我的服务.yml文件:

services:
itr_login.form.type.customer:
    class: itrLoginBundleFormTypeCustomerType
    tags:
        - { name: form.type, alias: customer }
itr.twig.timeConvert_extension:
        class: itrLoginBundleTwigtimeConvertExtension
        tags:
            - { name: twig.extension }
itr.login.default.controller:
        class: itrLoginBundleControllerDefaultController
itr.login.customer.controller:
            class: itrLoginBundleControllerCustomerController
itr.login.mail.controller:
            class: itrLoginBundleControllerMailController
itr.login.pdf.controller:
                class: itrLoginBundleControllerPdfController
itr.login.report.controller:
                class: itrLoginBundleControllerReportController
customer_repository:
        class: itrLoginBundleRepositorycustomerRepository
        arguments:
            dbal: "@doctrine.dbal.default_connection"
report_repository:
        class: itrLoginBundleRepositoryreportRepository
        arguments:
            dbal: "@doctrine.dbal.default_connection"
token_repository:
            class: itrLoginBundleRepositorytokenRepository
            arguments:
                dbal: "@doctrine.dbal.default_connection"

还有一个工作示例:

 try {
                $customer = $this->customerRepository->findFirstWithMoreThan30DaysAndActiveAndStatusAndMailday(date('j'));
                if ($customer != null) {
                    $this->customerRepository->updateStatus($customer, 5);
                    //haal data op en stop ze in databank
                    $tijd = new DateTime();
                    $report = new Report(null, null, null, null, $customer, $tijd->format("Y-m-d H:i:s"));
                    $report->setData($analytics);
                    $this->reportRepository->insertReport($report);
                    $this->customerRepository->updateStatus($customer, 10);
                    //genereer PDF
                    //HERE IS THE EXAMPLE LINE, that IS working properly
                    $this->get('itr.login.pdf.controller')->generate1PDF($customer);
                    $this->customerRepository->updateStatus($customer, 20);
                    //stuur mail
                    $this->get('itr.login.mail.controller')->send1Mail($customer);
                    $this->customerRepository->updateStatus($customer, 30);
                    $this->customerRepository->updateStatus($customer, 0);
                    $tweet = 'De klant is succesvol behandeld!';
                } else {
                    $tweet = 'Er is geen enkele klant gevonden waarvan: het laatste rapport meer dan 30 dagen geleden opgehaald is, er momenteel geen rapport in verwerking is, de huidige status actief is, én de huidige dag overeen komt met de door de klant aangegeven dag om te mailen.';
                }
                return $this->redirect($this->generateUrl('mainPage', array('tweet' => $tweet)));
            }
我已经

找到了一个解决方案,所以我想我会把它发布在这里供未来的读者使用:

有人向我指出,我不应该只让每个控制器都成为服务,这样我就可以从任何控制器访问所有方法,因为这是一种不好的做法。

因此,我听从了同事的建议,构建了一个包含"generate1PDF"和"prepeareReport"方法的服务。我可以从任何控制器调用这些方法(假设我在其中注入了服务),所以我的结构得到了改进,我的问题也得到了解决。

最新更新