我试图在我的Symfony 3项目上创建一个cron。
我在我的控制器上创建了用于保存XML文件数据的功能,当我在控制台上调用它时,我有一个错误,我不明白这一点。
这是我的第一个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 for bundle
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);
}
}
答案 0 :(得分:0)
你能解释为什么你在这里使用'控制器'吗?为什么你不在Command类中完成所有工作?
否则创建一个服务注入实体管理器和容器('@ service_container')并在那里工作。