本文介绍了如何在zend Framework 2或AjaxContext中使用Ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AjaxContext助手在ZF1中是一个很好的功能,我在很多地方都使用过它.

AjaxContext helper was a neat feature in ZF1 and i used it in many places.

我想知道ZF2中是否可用.

I was wondering if this is available in ZF2.

我做了一个测试并添加:

I did a test and added:

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

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

in the controller, added a action:

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

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

文件input.ajax.phtml

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

请求通过200代码通过ok,但出现渲染错误

The request goes through ok, with a 200 code, but i get a render error

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

任何想法出了什么问题,或者是另一种选择?谢谢.

any ideas what went wrong, or maybe an alternative ? thanks.

如果我这样做,请使用DefaultRenderingStrategy:

if i do this, using the DefaultRenderingStrategy:

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

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

edit2:

由于@Sam,我设法使其正常运行.这是我的步骤:

i managed to make it working thanks to @Sam. Here are my steps:

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>

谢谢

推荐答案

查看 JsonStrategy .

此外,如果您希望返回完整视图(局部布局),只需返回具有$viewModel->setTerminal(true)的ViewModel即可.

Furthermore, if you want the full view (apart layout) to be returned, simply return a ViewModel with $viewModel->setTerminal(true) that's all there is to it.

这篇关于如何在zend Framework 2或AjaxContext中使用Ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-25 22:54
查看更多