[英]Crontab with my Controller on Symfony3
我試圖對我的Symfony 3項目做出表率。
我的控制器上有make函數,用於保存XML文件中的數據,當我在控制台上調用它時,出現錯誤,我不理解。
這是我的第一個Symfony項目,我不知道我的代碼有什么問題。
我已經看過這個解決方案,但無法解決我的問題
當我運行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);
}
}
您能在這里解釋為什么要使用“控制器”嗎? 為什么不執行Command類中的所有工作?
否則,創建一個將實體管理器和容器('@service_container')注入其中的服務,然后您就可以工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.