2011-03-18 56 views
1

我是zend的新手,我找不到在zend中实现Ajax的方法。如何使用Ajax实现Zend路由

在一般的PHP中,它很容易做出一个Ajax请求,并在我们页面的所需部分显示响应。但即将到来,我不知道该怎么做。

假设在我的索引控制器的index.phtml文件中,我有一个按钮,当我点击它时,我必须向特定的控制器和动作发出ajax请求,并在我的页面中加载相关的控制器动作视图。

但我不明白的是如何为ajax请求指定url以及路由如何工作。

目前,我提出AJAX请求静态加载视图这样的:

xmlhttp.open( “GET”, “../应用/视图/脚本/注册/ register.phtml”,真);

仅供参考,我在我的应用程序中使用正则表达式路由,那么使用curl路由请求会更好吗?

回答

2

除了其他的答案说了什么,还有一个URL视图助手功能可以调用一个控制器上的特定行动是有益的。因此,您只需使用$this->url(array('controller' => 'your_controller', 'action' => 'your_action'), 'default', true);即可在“your_controller”控制器上使用“your_action”操作的链接(使用默认路由)。如果您有一个定义,您也可以指定一个特定的路线而不是“默认”。

因此,对于你的榜样,你会使用像在你看来PHTML文件以下(如果你使用的是默认的路由):

xmlhttp.open("GET","<?php echo $this->url(array('controller' => 'register', 'action' => 'register'), 'default', true);?>"); 

欲了解更多信息,请参阅Zend Framework - View Helpers

7

首先,你不要直接请求视图。您需要请求特定的控制器动作,例如

/register/register 

的Zend附带一个名为AjaxContext一个伟大的动作助手。此帮助程序可让您根据请求类型(XmlHttpRequest)和format参数以不同视图进行响应,禁用通常存在的任何布局。

要设置它,把这样的事情在你的控制器的init()方法

public function init() 
{ 
    $this->_helper->ajaxContext->addActionContext('register', 'html') 
           ->initContext(); 
} 

然后,添加一个视图脚本与ajax后缀,如register/register.ajax.phtml

构建您的AJAX GET请求包括format=html参数,如

xmlhttp.open('GET', '/register/register/format/html', true); 

xmlhttp.open('GET', '/register/register?format=html', true); 

什么将被退回是register.ajax.phtml所呈现的内容,没有任何布局。

1

你不应该直接请求视图。这是错误的。请求URI是这样的:

xmlhttp.open("GET","/register/register"); 

这意味着“我找默认的模块,注册控制器和登记行动”,或者换句话说RegisterController :: registerAction()。

这是一样的:

xmlhttp.open("GET","/default/register/register"); 

这是相同的,则默认模块可以被省略。

Zend Framework知道在哪里寻找视图脚本(除非你使用了一些不寻常的目录结构)。

您只需创建一个空白布局,并用它来开发Ajax控制器动作(或什么菲尔建议,AjaxContent可能是更好的为这个)。

1

我会写在这里几乎完全“如何”引导来实现AJAX在Zendframework 3.调用 我们需要:

  1. 一个路由声明
  2. 控制器
  3. 一些JavaScript(是出skope的,也许我会在其他地方表现出来)

路线声明:

<?php 
// editing a module.config.php in your project 
// typical lines: 
namespace Ajax2l; // my ajax module name 
use Zend\Router\Http\Segment; 
// ... 
'router' => [ 
    // ... other routes 
    'ajax2lsajaxcall' => [ 
    'type' => Segment::class, 
    'options' => [ 
     'route' => '/ajax2l/ajaxrequest[/:action][/:garbage]', 
     // the :action wildcard is a place to have extra parameters 
     // the :garbage wildcard is a place to send random strings 
     //  to ensure you break cache to get real process for 
     //  all your calls, since every call will be different 
     'defaults' => [ 
      'controller' => Controller\AjaxController::class, 
      'action'  => 'ajaxrequest'       
     ] 
    ], 
    'may_terminate' => true, 
    'child_routes' => [ 
     // other ajax routes if needed 
    ] 
], 
// I only use one ajax route and one controller for all my sites' ajax 
// calls because they fire small db actions and reflect them on small 
// parts of my pages. So I think I do not need a route and a controller 
// for each ajax call. Instead, I use a Library and some services to get 
// sets of elementary components and to perform tasks. 

的控制器 我Ajax2l模块位于 “myzend_project /模块” 目录下。

<?php 
// Filename: /module/Ajax2l/src/Controller/AjaxController.php 
namespace Ajax2l\Controller 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
// No more components are needed 

class AjaxController extends AbstractActionController { 
    public function ajaxrequestAction(){ 
    $request = $this->getRequest(); 
    $content = $request->getContent(); 
    $isHttpRequest = $this->ifIsValidHttpRequest(); 
    // if you have a real ajax call you must disable the normal output 
    $viewmodel = new ViewModel(); 
    $viewmodel->setTerminal($is_xmlhttprequest); 
    // If not an Ajax call (perhaps from untrusted eyes) serve a forgiven 
    // page message 
    if(!$isHttpRequest){ 
     return $this->forgivenPage(); 
    } 
    // Now prepare a response and responds 
    $requestParams = $this->preProcessRequest($content); 
    // call the logics to process your params 
    $output = $this->processRequestParams($requestParams); 
    // and send a response 
    $response = $this->getResponse(); 
    $response->setContent($output); 
    return $response; 
    } 

    private function ifIsValidHttpRequest($content){ 
    // I use a small token string to verify if posted data matches 
    // perhaps not the proper way but is fast and works 
    $token = 'my_secure_visitor_session_token_identifier'; 
    return (strpos($content, $token) > 2) ? 1 : 0; 
    // the validation is simplified here it has some more 
    // complex logics. To ensure validation of requesting source 
    } 

    private function preProcessRequest($content){ 
    // The logics to know what to do 
    // ... 
    // here you can identify and set properly the post params 
    } 

    function processRequestParams($requestParams){ 
    // some logics to process your data 
    // ... 
    // some logics to create a Json output 
    // ... 
    return $jsonObject; 
    } 

    protected function forgivenPage(){ 
    $view = new ViewModel([]); 
    // set a forgiven message page as output 
    $view->setTemplate('ajax2l/messages/forgiven.phtml'); 
    // note: the views directory uses lowercase for module names 
    return $view; 
    } 
} 

希望它有帮助。我失去了大量的时间阅读Zend Ajax没有结果。所以我希望我是最后一次在这个话题上失去时间。

Luis