微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有页面类型的Typo3 Extbase AJAX

有没有办法在不使用pageNum页面的情况下在Extbase扩展中创建AJAX调用
编辑:

Helmut Hummel是TYPO3 CMS团队的成员,measured使用EID和Extbase比使用typeNum方法慢.但由于typeNum方法配置起来很麻烦,因此他开发了第三种方法.

扩展typoscript_rendering提供了一种直接调用Extbase操作的方法,无需额外配置.它包含一个生成此类链接的ViewHelper,可以在Fluid模板中使用:

{namespace h=Helhum\TyposcriptRendering\ViewHelpers}
<script>
var getParticipationsUri = '<h:uri.ajaxAction controller="Participation" action="listByCompetition" arguments="{competition:competition}" />';
</script>

这会生成一个URI,该URI调用我的“ParticipationController”的动作“listByCompetition”.您可以正常传递参数.

唯一的缺点是出于安全原因,扩展使用cHash来验证请求参数. cHash由GET提交,但您不能同时通过GET传递其他参数,因为它会使cHash无效.因此,如果要在此类请求中传递表单数据,则需要混合GET(用于有效的AJAX调用)和POST(用于提交用户数据):

<script>
var createAddressUri = '<h:uri.ajaxAction controller="Address" action="create" />';
$body.on('submit','#myForm',function(e) {
    e.preventDefault();
    emailAddress = $('#myForm').find('#email');
    if (typeof(emailAddress) === 'string') {
        $.ajax({
            url: createAddressUri,type: 'POST',data: { 'tx_myext_pluginname[address][email]' : emailAddress},success: function() {
              // things to do on success
            }
        })
    }
});
</script>

(当然这只是一个非常基本的例子.你可以发布整个模型等)

EID方式:

是的,您可以使用EID(扩展ID)机制.官方声明没有哪种方式(pageType或eID)应该用于Extbase AJAX调用,它似乎只是一个品味问题.

一个很好的教程,可以找到here,我在这里复制源代码

<?PHP

/** *************************************************************
 *
 * Extbase dispatcher for Ajax Calls TYPO3 6.1 namespaces
 *
 * IMPORTANT Use this script only in Extensions with namespaces
 *
 * Klaus Heuer <klaus.heuer@t3-developer.com>
 *
 * This script is part of the TYPO3 project. The TYPO3 project is
 * free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License,or
 * (at your option) any later version.
 *
 * The GNU General Public License can be found at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * This script is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or fitness FOR A PARTIculaR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This copyright notice MUST APPEAR in all copies of the script!
 * ************************************************************* */

/** ************************************************************
 * Usage of this script:
 *
 * - copy this script in your Extension Dir in the Folder Classes
 * - Set the vendor and Extension Name in Line 82 + 83
 * - Include the next line in the ext_localconf.PHP,change the ext name!
 * - $TYPO3_CONF_VARS['FE']['eID_include']['ajaxdispatcher'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('myExtension').'Classes/Eiddispatcher.PHP';
 *
 * Use for Ajax Calls in your jQuery Code:
 *
 *     $('.jqAjax').click(function(e)  {
 *       var uid = $(this).find('.uid').html();
 *       var storagePid = '11';
 *      
 *       $.ajax({
 *           async: 'true',*           url: 'index.PHP',*           type: 'POST',*        
 *           data: {
 *               eID: "ajaxdispatcher",*               request: {
 *                   pluginName:  'patsystem',*                   controller:  'Todo',*                   action:      'findTodobyAjax',*                   arguments: {
 *                       'uid': uid,*                       'storagePid': storagePid
 *                   }
 *               }
 *           },*           dataType: "json",*          
 *           success: function(result) {
 *               console.log(result);
 *           },*           error: function(error) {
 *              console.log(error);               
 *           }
 *       });
 *************************************************************** */


/**
 * Gets the Ajax Call Parameters
 */
$ajax = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('request');

/**
 * Set vendor and Extension Name
 *
 * vendor Name like your vendor Name in namespaces
 * ExtensionName in upperCamelCase
 */
$ajax['vendor'] = 'T3Developer';
$ajax['extensionName'] = 'ProjectsAndTasks';

/**
 * @var $TSFE \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
 */
$TSFE = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController',$TYPO3_CONF_VARS,0);
\TYPO3\CMS\Frontend\Utility\EidUtility::initLanguage();

// Get FE User information
$TSFE->initFEuser();
// Important: no Cache for Ajax stuff
$TSFE->set_no_cache();

//$TSFE->checkAlternativCoreMethods();
$TSFE->checkAlternativeIdMethods();
$TSFE->determineId();
$TSFE->initTemplate();
$TSFE->getConfigArray();
\TYPO3\CMS\Core\Core\Bootstrap::getInstance()->loadConfigurationAndInitialize();

$TSFE->cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
$TSFE->settingLanguage();
$TSFE->settingLocale();

/**
 * Initialize Database
 */
\TYPO3\CMS\Frontend\Utility\EidUtility::connectDB();

/**
 * @var $objectManager \TYPO3\CMS\Extbase\Object\ObjectManager
 */
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');


/**
 * Initialize Extbase bootstap
 */
$bootstrapConf['extensionName'] = $ajax['extensionName'];
$bootstrapConf['pluginName'] = $ajax['pluginName'];

$bootstrap = new TYPO3\CMS\Extbase\Core\Bootstrap();
$bootstrap->initialize($bootstrapConf);

$bootstrap->cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tslib_cObj');

/**
 * Build the request
 */
$request = $objectManager->get('TYPO3\CMS\Extbase\Mvc\Request');

$request->setControllervendorName($ajax['vendor']);
$request->setcontrollerExtensionName($ajax['extensionName']);
$request->setPluginName($ajax['pluginName']);
$request->setControllerName($ajax['controller']);
$request->setControllerActionName($ajax['action']);
$request->setArguments($ajax['arguments']);

$response = $objectManager->create('TYPO3\CMS\Extbase\Mvc\ResponseInterface');

$dispatcher = $objectManager->get('TYPO3\CMS\Extbase\Mvc\dispatcher');

$dispatcher->dispatch($request,$response);

echo $response->getContent();
//die();
?>

查看“使用此脚本”部分,了解如何注册eID.该脚本适用于TYPO3 6.1及更高版本.

原文地址:https://www.jb51.cc/ajax/160180.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐