Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何构造/设计Web应用程序,最大限度地提高AJAX/内联编辑的可重用性_Php_Ajax_Zend Framework_Web Applications - Fatal编程技术网

Php 如何构造/设计Web应用程序,最大限度地提高AJAX/内联编辑的可重用性

Php 如何构造/设计Web应用程序,最大限度地提高AJAX/内联编辑的可重用性,php,ajax,zend-framework,web-applications,Php,Ajax,Zend Framework,Web Applications,如何构造/设计web应用程序,以便可以重用代码进行AJAX/内联编辑。我使用MVC(Zend框架)。有些部件已经有助于重用 视图/布局-我可以禁用布局(包含&stuff的“页面模板”),只为在AJAX对话框中使用的该操作提供部分视图 但是对于内联编辑,我需要提供更小的部分,只有文本框。此外,我需要一个特殊的操作来处理请求(例如,updateName()而不是updateUser()) 设计我的应用程序以最大限度地提高可重用性的方法是什么?您需要使用上下文切换器。一个简单的例子 // In s

如何构造/设计web应用程序,以便可以重用代码进行AJAX/内联编辑。我使用MVC(Zend框架)。有些部件已经有助于重用

  • 视图/布局-我可以禁用布局(包含
    &stuff的“页面模板”),只为在AJAX对话框中使用的该操作提供部分视图
  • 但是对于内联编辑,我需要提供更小的部分,只有文本框。此外,我需要一个特殊的操作来处理请求(例如,
    updateName()
    而不是
    updateUser()

设计我的应用程序以最大限度地提高可重用性的方法是什么?

您需要使用
上下文切换器。一个简单的例子

// 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();    
}
然后需要定义两个操作
remove
view

remove
将返回JSON编码数组中的所有视图变量,javascript可以解析和使用这些变量,因此不需要视图脚本。使用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操作
视图
将需要一个视图脚本,以下是操作

public function viewAction()
{
     // Load something from the database and assign to $data
     $this->view->name = $data['name']
}
您的视图脚本将被称为
view.ajax.phtml
这将包含
view
操作的输出,您可以访问URL上的视图操作,如
index/view/format/html

视图脚本可能如下所示

<h1>Hello <?=$this-escape($this->name)?></h1>
<p>Welcome to my site. This was obtained via an AJAX request.</p>
你好 欢迎来到我的网站。这是通过AJAX请求获得的

记得读这本书,因为我可能遗漏了什么


我希望这能有所帮助。

您需要使用
上下文切换器。一个简单的例子

// 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();    
}
然后需要定义两个操作
remove
view

remove
将返回JSON编码数组中的所有视图变量,javascript可以解析和使用这些变量,因此不需要视图脚本。使用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操作
视图
将需要一个视图脚本,以下是操作

public function viewAction()
{
     // Load something from the database and assign to $data
     $this->view->name = $data['name']
}
您的视图脚本将被称为
view.ajax.phtml
这将包含
view
操作的输出,您可以访问URL上的视图操作,如
index/view/format/html

视图脚本可能如下所示

<h1>Hello <?=$this-escape($this->name)?></h1>
<p>Welcome to my site. This was obtained via an AJAX request.</p>
你好 欢迎来到我的网站。这是通过AJAX请求获得的

记得读这本书,因为我可能遗漏了什么


我希望这会有所帮助。

查看关于contextSwitch的ZF文档,获取xml/json部分html内容的专用视图(和操作)。查看关于contextSwitch的ZF文档,获取xml/json部分html内容的专用视图(和操作)。