2010-11-30 77 views
2

我覺得需要允許瀏覽器後退按鈕工作,以及允許用戶將他們看到的內容加入書籤。如何在ZF上通過Ajax調用實現「Deep Link」?

我在Zend路線上不是多功能的,但是現在我無法改變它。

這是Ajax實現的辦法,我使用:

class TestController extends Zend_Controller_Action { 

public function init() 
{ 
    /* Initialize action controller here */ 
    if ($this->getRequest()->isXMLHttpRequest()) { 
     $this->_helper->layout()->setLayout('blank'); 
     $logger = $this->getInvokeArg('bootstrap')->getResource('Log'); 
     $logger->debug('AJAX Call'); 
    } 
} 


public function indexAction() 
{ 
    // render the default page 
} 

public function somethingelseAction() 
{ 
    // do something else render something. 
} 

}

有我的初步看法渲染目標DIV,以及一些鏈接...這是我的指標。 phtml:

<h1>Tests...</h1> 
     <a class="ajaxloader" 
      href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'speed'), null, true);?>">Speed</a> 
     <a class="ajaxloader" 
      href="<?php echo $this->url(array('controller'=> 'test', 'action' => 'somethingelse'), null, true);?>">Something Else</a> 
     <div id="testresults"> 
     <h1>Default stuff to show.</h1> 
     </div> 

一些jQuery代碼附加到這些'ajaxloader'鏈接並將結果定位到'testresults'div。

$(function() { 
$('.ajaxloader').click(function(event) { 
    var target = $(this).attr('href'); 
    window.location.hash = target; 
    $('#testresults').fadeOut('slow', function() { 
     // complete fadeout, load new content while it's hiding! 
     $.ajax({ 
      url : target, 
      success : function(data) { 
       $('#testresults').html(data); 
       $('#testresults').fadeIn(); 
      } 
     }); 
    }); 
    return false; 
}) 
}); 

這裏如何實現深層鏈接?

非常感謝, MEM

PS - 好信貸這個實現去達里爾E.克拉克。 我可以採取壞的。

+0

其他人都讀過這個問題,並認爲'好吧,如果你可以使用選擇公理'會更容易?沒有?好吧,只有我。 – 2010-11-30 14:23:08

回答

1

對於深度鏈接:我必須做同樣的事情(我們的代碼看起來幾乎相同!),並在一段時間後發現jQuery Address。但IE瀏覽器存在問題。其中一個功能導致它停止工作。據我所知,我沒有使用該功能,所以我只是讓它回來,而不是做任何事情。在當前版本(1.3.1)中,它是第77行的函數(_search = function(el))。就像我所說的,我只是在函數的頂部放置了一個返回值,現在它很好地工作。

至於路線... The documentation應該是您的第一個停靠港。 我的路線做的,就是在我的引導文件,創建一個_init功能,這樣做:

$this->bootstrap('frontController'); 
/* @var $frontcontroller Zend_Controller_Front */ 
$frontcontroller = $this->getResource('frontController'); 
$router = $frontcontroller->getRouter(); 
$router->addRoute(
    'page', 
    new Zend_Controller_Router_Route_Regex('(.*)\.html', 
     array('controller' => 'index', 
     'action' => 'page', 
      'module' => 'default'), 
      array('page' => 1), 
     '%s.html' 
    ) 
); 

但整理了一下自己的路線,以滿足您的需求(例如,你可能不希望使用正則表達式路由)。