how to use ajax in zend framework 2 or the AjaxCon

2019-04-02 14:24发布

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

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];
}

a file input.ajax.phtml

and a ajax call: $.get('/form/input/format/html').css("display","block");

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.

edit:

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); i'll get null

edit2:

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>';

the result will be <div>John</div>

thanks

1条回答
我命由我不由天
2楼-- · 2019-04-02 15:10

Check out the JsonStrategy.

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.

查看更多
登录 后发表回答