简体   繁体   English

我的控制器在Symfony3上的Crontab

[英]Crontab with my Controller on Symfony3

I tried to make a cron on my Symfony 3 project. 我试图对我的Symfony 3项目做出表率。

I have make function on my controller for save data from XML file and when I what to call it on the console I have an error and I don't understand this. 我的控制器上有make函数,用于保存XML文件中的数据,当我在控制台上调用它时,出现错误,我不理解。

It's my first Symfony project and I dont know what is wrong on my code. 这是我的第一个Symfony项目,我不知道我的代码有什么问题。

I've look this solution but I can't solve my probleme 我已经看过这个解决方案,但无法解决我的问题

This is error from console when I run php bin/console app:save-xml-data 当我运行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

My Controller 我的控制器

<?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 );
        //....

    }



}

My services.yml for the bundle 捆绑包的我的services.yml

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

My commande 我的命令

<?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);
    }
}

Could you explain why you are using a 'Controller' here ? 您能在这里解释为什么要使用“控制器”吗? Why you dont do all the work in your Command class ? 为什么不执行Command类中的所有工作?

Otherwise create a service inject the entity manager and container ('@service_container') there and do you work. 否则,创建一个将实体管理器和容器('@service_container')注入其中的服务,然后您就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM