contextSwitcher
を使用する必要があります。簡単な例
// In side your controller class
public function init()
{
// Obtain the contextSwitcher
$ajaxContext = $this->_helper->getHelper('AjaxContext');
// Add two new contexts, remove which will be JSON and view which will be HTML
$ajaxContext->addActionContext('remove', 'json')
->addActionContext('view', 'html');
// Init the context
$ajaxContext->initContext();
}
あなたは、2つのアクションremove
とview
remove
を定義する必要があなたのJavascriptを解析し、使用することができますJSONエンコードされたアレイ内のすべてのビュー変数を返すために起こっている、あなたはそれゆえ行いますビュースクリプトは必要ありません。 JSONやHTMLのようなコンテキストを使用するアクションは自動的にそのレイアウトを無効にします。
public function removeAction()
{
// Do some operations here for removing data, and I'll assume you assign the outcome to $success
if($success)
$this->view->message = "Success";
else
$this->view->message = "There was a problem removing your data";
}
あなたは/index/remove/format/json
のようなURLを使用してremove
アクションにアクセスする - フォーマットJSONなしで要求が失敗します。
あなたのHTMLアクションview
を使用すると、URLにビューアクションにアクセスし、ここで
public function viewAction()
{
// Load something from the database and assign to $data
$this->view->name = $data['name']
}
あなたのビュースクリプトが、これはview
アクションのあなたの出力が含まれていますview.ajax.phtml
呼ばれるアクションがあり、ビュースクリプトが必要になりますindex/view/format/html
のようになります。
ビュースクリプトは、私が何かを見逃しているかもしれとしてdocs on the contextSwitcherを読むことを忘れないでください、この
<h1>Hello <?=$this-escape($this->name)?></h1>
<p>Welcome to my site. This was obtained via an AJAX request.</p>
のように見えることがあります。
私は役立つことを願っています。
xml/jsonの部分的なHTMLコンテンツの特殊なビュー(およびアクション)を取得するために、contextSwitchに関するZFドキュメントを確認してください。 – regilero