如何使用Zend Framework通过Ajax加载视图?

时间:2022-02-02 20:18:22

Is it possible to load views with ajax in the zend framework, that way the layout page doesn't get refreshed, just the main content div?

是否可以在zend框架中使用ajax加载视图,这样布局页面不会刷新,只是主要的内容div?

2 个解决方案

#1


5  

use Ajax context switching you can do it by adding this to your init function in your controller

使用Ajax上下文切换,您可以通过将其添加到控制器中的init函数来实现

public function init()
{
 $ajaxContext = $this->_helper->getHelper('AjaxContext');
 $ajaxContext->addActionContext('my', 'html') //my is your action
                ->initContext();
}

The html parameter is the type of Ajax request. it can also be JSON or XML

html参数是Ajax请求的类型。它也可以是JSON或XML

public function myAction() {
    // get what you are sending to your view : data
    $this->view->data = $data;
}

create a view my.ajax.phtml to which which myAction will attempt to render to it by default and then include my.ajax.phtml in your view (your main content div)

创建一个my.ajax.phtml视图默认情况下myAction将尝试向其显示,然后在视图中包含my.ajax.phtml(您的主要内容div)

#2


0  

With Zend 1.12 we used the Zend_Controller_Action_Helper_Json;

使用Zend 1.12,我们使用了Zend_Controller_Action_Helper_Json;

Controller:

控制器:

use Zend_Controller_Action_Helper_Json;

class MyController extends Zend_Controller_Action {
    public function init() {
        Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_Json());
    }

    public function fooAction() {
        $this->getResponse()->setHttpResponseCode(200);
        $this->_helper->json(array('value' => 1));
    }
}

View:

视图:

  • No view file
  • 没有视图文件

Output:

输出:

{"value":1}

Call:

呼叫:

http://example/my/foo

#1


5  

use Ajax context switching you can do it by adding this to your init function in your controller

使用Ajax上下文切换,您可以通过将其添加到控制器中的init函数来实现

public function init()
{
 $ajaxContext = $this->_helper->getHelper('AjaxContext');
 $ajaxContext->addActionContext('my', 'html') //my is your action
                ->initContext();
}

The html parameter is the type of Ajax request. it can also be JSON or XML

html参数是Ajax请求的类型。它也可以是JSON或XML

public function myAction() {
    // get what you are sending to your view : data
    $this->view->data = $data;
}

create a view my.ajax.phtml to which which myAction will attempt to render to it by default and then include my.ajax.phtml in your view (your main content div)

创建一个my.ajax.phtml视图默认情况下myAction将尝试向其显示,然后在视图中包含my.ajax.phtml(您的主要内容div)

#2


0  

With Zend 1.12 we used the Zend_Controller_Action_Helper_Json;

使用Zend 1.12,我们使用了Zend_Controller_Action_Helper_Json;

Controller:

控制器:

use Zend_Controller_Action_Helper_Json;

class MyController extends Zend_Controller_Action {
    public function init() {
        Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_Json());
    }

    public function fooAction() {
        $this->getResponse()->setHttpResponseCode(200);
        $this->_helper->json(array('value' => 1));
    }
}

View:

视图:

  • No view file
  • 没有视图文件

Output:

输出:

{"value":1}

Call:

呼叫:

http://example/my/foo