如何使用Zend框架2或AjaxContext AJAX?(how to use ajax in z

2019-08-02 06:20发布

AjaxContext帮手是ZF1一个整洁的功能,我把它用在很多地方。

我在想,如果这是ZF2可用。

我做了一个测试,并补充说:

public function init()
{
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('input', 'html')
                ->addActionContext('number', 'html')
                ->initContext();
}

在控制器中,增加了一个动作:

public function inputAction()
{
    $form = new AddInput();

    return ['form' => $form];
}

文件input.ajax.phtml

和AJAX调用: $.get('/form/input/format/html').css("display","block");

该请求经过确定,有200码,但我得到一个错误渲染

Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "form/index/input"; resolver could not resolve to a file' in C:\xampp\htdocs\Zend-Project\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 454

( ! ) Zend\View\Exception\RuntimeException: Zend\View\Renderer\PhpRenderer::render: Unable to render template "form/index/input"; resolver could not resolve to a file in C:\xampp\htdocs\Zend-Project\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 454

#   Time    Memory  Function    Location
1   0.0003  139048  {main}( )   ..\index.php:0
2   0.0969  4288136 Zend\Mvc\Application->run( )    ..\index.php:12
3   0.1463  6125720 Zend\Mvc\Application->completeRequest( )    ..\Application.php:310
4   0.1463  6125832 Zend\EventManager\EventManager->trigger( )  ..\Application.php:326
5   0.1463  6125904 Zend\EventManager\EventManager->triggerListeners( ) ..\EventManager.php:208
6   0.1464  6127112 call_user_func ( )  ..\EventManager.php:468
7   0.1464  6127128 Zend\Mvc\View\Http\DefaultRenderingStrategy->render( )  ..\EventManager.php:468
8   0.1464  6127176 Zend\View\View->render( )   ..\DefaultRenderingStrategy.php:128
9   0.1465  6128304 Zend\View\View->renderChildren( )   ..\View.php:196
10  0.1465  6128936 Zend\View\View->render( )   ..\View.php:231
11  0.1466  6129560 Zend\View\Renderer\PhpRenderer->render( )   ..\View.php:203

任何想法出了什么问题,也许别的选择吗? 谢谢。

编辑:

如果我这样做,使用DefaultRenderingStrategy

public function inputAction()
{
    $result = new ViewModel(array('some_parameter' => 'some value',));
    $result->setTerminal(true);
    return $result;
}

var_dump($this->result); 我会得到null

EDIT2:

我设法使它工作感谢@Sam。 下面是我的步骤:

javascript

$.get('/form/input', { name: "John", time: "2pm" }).done(function(data) {
    $('#some_div').append(data);
});

controller

public function inputAction()
{
    $request = $this->getRequest();
    $results = $request->getQuery();  // this is the get string

    $result = new ViewModel(['result' => $results]);
    $result->setTerminal(true);

    return $result;
}

view

<?php
echo '<div>'.$this->result->name.'</div>';

其结果将是<div>John</div>

谢谢

Answer 1:

退房JsonStrategy 。

此外,如果你想在众目睽睽之下将返回(除了布局),简单地返回一个视图模型$viewModel->setTerminal(true)这一切就是这么简单。



文章来源: how to use ajax in zend framework 2 or the AjaxContext?