2017-10-11 6 views
0

Symfony 3プロジェクトでcronを作成しようとしました。私のコントローラーをSymfony3で使用するCrontab

XMLファイルのデータを保存するために私のコントローラーに関数を作成しました。コンソール上で何を呼び出すべきか私はエラーがあり、これはわかりません。

これは私の最初のSymfonyプロジェクトです。私のコードで何が間違っているのか分かりません。私はPHPのbin /コンソールアプリケーションを実行したときに

を私はthis solutionを見てきましたが、私は私のproblemeを解決することはできませんこれは、コンソールからのエラーです:保存-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 

マイコントローラー

バンドル

services: 
    core_layout.setxmldata: 
     class: Core\LayoutBundle\Controller\LayoutController 
     arguments: ['@doctrine.orm.entity_manager'] 
のための私のservices.yml
<?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); 
     //.... 

    } 



} 

あなたはここに 'コントローラ' を使用している理由

マイ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); 
    } 
} 
+0

[Minimal](https://stackoverflow.com/help/mcve)のサンプルはどうですか? – svgrafov

+0

@svgrafov:投稿を編集した方がいいかもしれない。 –

+0

私が提供したリンクを実際に読んだことはありますか?あなたの例ではコードの量を減らそうとするべきです。あなたの推測はまったく助けません。 – svgrafov

答えて

0

あなたが説明してもらえますか?なぜあなたはCommandクラスのすべての仕事をしないのですか?

それ以外の場合は、エンティティマネージャとコンテナ( '@service_container')を挿入してサービスを作成してください。

+0

Webでこれを実行したいので、コントローラを使用します。だから私は自分の機能をサービスにしなければならないのですか? –

+0

はいコントローラから呼び出せるサービスを作成します($ this-> container-> get( 'service'))とあなたのコマンド($ this-> getApplication() - > getKernel() - > getContainer ) - > get( 'service'))、すべての作業を行います。 –

+0

Thxあなたの助言のために。私はコントローラを変更してサービスを作ったが、今はすべて動作する! –

関連する問題