douhu2890 2017-01-04 07:58
浏览 36
已采纳

从PHP / TYPO3 Extbase中的ActionController调用CommandController

I wrote a Command Controller that handles data import from an URL. pseudo-syntax is like this:

class ImportCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
{
  public function importCommand($auth){
    $data = file_get_content();
  }
}

this works. But when I try to call that command from the Action Controller of my backend Module I get errors. Heres the code: ActionController:

class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * @var \Vendor\MyExt\Command\ImportCommandController importCommandCtrl
     * @inject
     */
    protected $importCommandCtrl;

    public function importAction()//($url,$usr,$pass)
    {
        //$this->importCommandCtrl = GeneralUtility::makeInstance('Vendor\MyExt\Command\ImportCommandController');
        $this->importCommandCtrl->testCommand();
    }
}

When I call importAction() like this, I get:

"Call to a member function testCommand() on null"

When I uncomment the makeInstance, I get:

"Call to a member function get() on null"

Sadly, this topic is documente rather poorly in the TYPO3 Docs. Can someone help me on this or point me to the right direction?

  • 写回答

2条回答 默认 最新

  • dongzang7182 2017-01-04 09:59
    关注

    I'd like to slightly alter the answer already given by René and add some code examples. I also recommend to put your import logic into a dedicated class, e.g. ImportService:

    namespace Vendor\MyExt\Service;
    use TYPO3\CMS\Core\SingletonInterface;
    class ImportService implements SingletonInterface
    {
        public function importData()
        {
           // import logic goes here
        }
    }
    

    You can now inject this class as a dependency of your CommandController and your ActionController:

    class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
    {
        /**
         * @var \Vendor\MyExt\Service\ImportService
         * @inject
         */
         protected $importService;
    
        public function importAction()
        {
            $this->importService->importData();
        }
    }
    
    class ImportCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandControlle
    {
        /**
         * @var \Vendor\MyExt\Service\ImportService
         * @inject
         */
        protected $importService;
    
        public function importCommand()
        {
            $this->importService->importData();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio