【发布时间】: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:是的,我读过它,我认为这已经足够了,但我想没有。我再次更改我的帖子,但现在可能太短了......
-
你不应该把所有的东西都删掉。相反,尝试降低复杂性,直到错误消失。比添加最小的结果,这有你的错误。