2011-03-18 73 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