【问题标题】:Crontab with my Controller on Symfony3crontab 和我在 Symfony3 上的控制器
【发布时间】:2018-03-23 02:10:00
【问题描述】:

我试图在我的 Symfony 3 项目上创建一个 cron。

我的控制器上有用于从 XML 文件保存数据的 make 函数,当我在控制台上调用它时出现错误,我不明白这一点。

这是我的第一个 Symfony 项目,我不知道我的代码出了什么问题。

我看过this solution,但我无法解决我的问题

这是我运行 php bin/console app:save-xml-data 时来自控制台的错误

PHP Fatal error:  Call to a member function get() on a non-object in /data/www/weatherperf/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 412

PHP   9. Symfony\Bundle\FrameworkBundle\Controller\Controller->get() /data/www/weatherperf/src/Core/LayoutBundle/Controller/LayoutController.php:125
[2017-10-11 13:14:38] php.CRITICAL: Fatal Error: Call to a member function get() on a non-object {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 0): Error: Call to a member function get() on a non-object at /data/www/weatherperf/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php:412)"}


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Error: Call to a member function get() on a non-object

我的控制器

<?php

class LayoutController extends Controller
{
    private $sNameController = "CoreLayoutBundleLayout";
    private $em = null;

    public function __construct( EntityManager $em )
    {
        $this->em = $em;
    }

    /**
     * @Route("/xml_uploadData", name="core_layout_xml_upload")
     */
    public function setXmlData()
    {
        $sTimestamp = date(time());

        $this->get('admin_layout.log')->addLogTo( "getData", "Début Traitement", "SaveData", $this->sNameController );
        //....

    }



}

我的 services.yml 包

services:
    core_layout.setxmldata:
        class: Core\LayoutBundle\Controller\LayoutController
        arguments: ['@doctrine.orm.entity_manager']

我的命令

<?php

namespace Core\LayoutBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class SetXmlDataCommand extends ContainerAwareCommand
{
    protected function configure()
    {

        // the name of the command (the part after "php bin/console")
        $this->setName('app:save-xml-data')
             ->setDescription('Save data from XML file') 
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // outputs a message to the console followed by a "\n"
        $output->writeln('Debut de la commande de sauvegarde');

        // access the container using getContainer()
        $saveService = $this->getContainer()->get('core_layout.setxmldata');
        $results = $saveService->setXmlData();

        $output->writeln($results);
    }
}

【问题讨论】:

  • Minimal 例子怎么样?
  • @svgrafov:我已经编辑了帖子,也许会更好
  • 你真的阅读过我提供的链接吗?您应该尝试减少示例中的代码量。而你的猜测根本没有帮助。
  • @svgrafov:是的,我读过它,我认为这已经足够了,但我想没有。我再次更改我的帖子,但现在可能太短了......
  • 你不应该把所有的东西都删掉。相反,尝试降低复杂性,直到错误消失。比添加最小的结果,这有你的错误。

标签: php symfony cron


【解决方案1】:

您能解释一下为什么在这里使用“控制器”吗?为什么你不在你的 Command 类中做所有的工作?

否则创建一个服务注入实体管理器和容器 ('@service_container') 那里你工作。

【讨论】:

  • 我使用我的控制器是因为我也想在网络上执行它。所以我必须让我的功能成为服务?
  • 是的,我会创建一个服务,您可以从您的控制器 ($this->container->get('service')) 和您的命令(使用 $this->getApplication()-> getKernel()->getContainer()->get('service')) 并在那里完成所有工作。
  • 感谢您的建议。我已经更改了控制器并提供了服务,现在一切正常!
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
相关资源
最近更新 更多