Typo3 Extbase AJAX without page typenum

前端 未结 5 533
不知归路
不知归路 2020-12-14 12:55

Is there any way to create AJAX calls in Extbase extension without using of page typeNum?

5条回答
  •  情歌与酒
    2020-12-14 13:54

    For Test Extension.

    Include EID in the ext_localconf.php file

    ## Ajax configuration
    $TYPO3_CONF_VARS['FE']['eID_include']['Test'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('test').'Classes/Ajax/EidDispatcher.php';
    

    Create directory in classes – Classes/Ajax/EidDispatcher.php

    namespace TYPO3\Test\Ajax;
    
    class EidDispatcher {
        /**
       * @var \array
       */
        protected $configuration;
    
        /**
       * @var \array
       */
        protected $bootstrap;
    
        /**
       * The main Method
       *
       * @return \string
       */
        public function run() {
            return $this->bootstrap->run( '', $this->configuration );
        }
    
        /**
       * Initialize Extbase
       *
       * @param \array $TYPO3_CONF_VARS
       */
        public function __construct($TYPO3_CONF_VARS) {
    
            $ajaxRequest = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_Test_addhours');
    
            // create bootstrap
            $this->bootstrap = new \TYPO3\CMS\Extbase\Core\Bootstrap();
    
            // get User
            $feUserObj = \TYPO3\CMS\Frontend\Utility\EidUtility::initFeUser();
    
            // set PID
            $pid = (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET( 'id' )) ? \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('id') : 1;
    
            // Create and init Frontend
            $GLOBALS['TSFE'] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( 'TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController', $TYPO3_CONF_VARS, $pid, 0, TRUE );
            $GLOBALS['TSFE']->connectToDB();
            $GLOBALS['TSFE']->fe_user = $feUserObj;
            $GLOBALS['TSFE']->id = $pid;
            $GLOBALS['TSFE']->determineId();
            $GLOBALS['TSFE']->getCompressedTCarray(); //Comment this line when used for TYPO3 7.6.0 on wards
            $GLOBALS['TSFE']->initTemplate();
            $GLOBALS['TSFE']->getConfigArray();
            $GLOBALS['TSFE']->includeTCA(); //Comment this line when used for TYPO3 7.6.0 on wards
    
            // Get Plugins TypoScript
            $TypoScriptService = new \TYPO3\CMS\Extbase\Service\TypoScriptService();
            $pluginConfiguration = $TypoScriptService->convertTypoScriptArrayToPlainArray($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_Test.']);
    
            // Set configuration to call the plugin
            $this->configuration = array (
                    'pluginName' => $ajaxRequest['pluginName'],
                    'vendorName' => 'TYPO3',
                    'extensionName' => 'Test',
                    'controller' => $ajaxRequest['controller'],
                    'action' => $ajaxRequest['action'],
                    'mvc' => array (
                            'requestHandlers' => array (
                                    'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler' => 'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler'
                            )
                    ),
                    'settings' => $pluginConfiguration['settings'],
                    'persistence' => array (
                            'storagePid' => $pluginConfiguration['persistence']['storagePid']
                    )
            );
    
        }
    }
    global $TYPO3_CONF_VARS;
    // make instance of bootstrap and run
    $eid = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( 'TYPO3\Test\Ajax\EidDispatcher', $TYPO3_CONF_VARS );
    echo $eid->run();
    

    Call From Script

    $.ajax({
        async: 'true',
        url: 'index.php',      
        type: 'GET', 
        data: {
            eID: "Test",  
            tx_ExtName_PluginName: {
                pluginName:  'Plugin_Name',
                controller:  'Controller_Name',
                action:      'Action_Name',
            }
        },
        success:function(data){
            // code
        }
    });
    

提交回复
热议问题