1

AjaxContexthelper 是 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

编辑2:

感谢@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>

谢谢

4

1 回答 1

3

查看JsonStrategy

此外,如果您希望返回完整视图(分离布局),只需返回一个 ViewModel$viewModel->setTerminal(true)即可。

于 2012-11-02T19:29:44.630 回答