用我的控制器在Symfony3上的Crontab
我试着在我的Symfony 3项目上做一个cron。用我的控制器在Symfony3上的Crontab
我有我的控制器上的功能来保存XML文件中的数据,当我在控制台上调用它时我有一个错误,我不明白这一点。
这是我的第一个Symfony项目,我不知道我的代码有什么问题。
我已经看this solution,但我解决不了我的probleme
这是从控制台的错误,当我运行PHP斌/控制台应用程序:保存的XML数据
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:
core_layout.setxmldata:
class: Core\LayoutBundle\Controller\LayoutController
arguments: ['@doctrine.orm.entity_manager']
services.yml
我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);
}
}
你能解释一下为什么您使用的是 '控制器' 在这里?为什么你不在Command类中做所有的工作?
否则创建一个服务在那里注入实体管理器和容器('@service_container')并且你工作。
我使用我的控制器,因为我也想在网络上执行此操作。所以我必须让我的功能成为一项服务? –
是的我会创建一个服务,你可以从你的控制器调用($ this-> container-> get('service'))和你的命令(使用$ this-> getApplication() - > getKernel() - > getContainer ) - > get('service'))并完成所有工作。 –
Thx为您的建议。我改变了我的控制器并提供服务,现在一切正常! –
[Minimal](https://*.com/help/mcve)例子如何? – svgrafov
@svgrafov:我编辑过这个帖子,也许它更好 –
你真的看过我提供的链接吗?您应该尝试减少示例中的代码量。而你的猜测根本没有帮助。 – svgrafov