如何在 PHP 中使用 MVC 处理 Ajax 请求
我现在正在用 PHP 开发我自己的 MVC 框架。我正在尝试弄清楚如何处理 Ajax 请求...
页面是使用 URI www.domain.com/controller/method/params 构建的
,因此 URI 通过路由器类传递,该路由器类获取控制器和方法+ 要使用的参数。
到目前为止,这听起来不错,但我还将有一个页眉和页脚,甚至在实际页面上也可能有一个侧边栏,并且 MVC 将填充页面的主要内容部分。
当我发出 Ajax 请求时,例如 www.domain.com/user/create :controller=user method=create
我会尝试再次构建整个页面,因为它正在向我的应用程序发送请求。
我很困惑如何正确处理这个问题?
I am working on my own MVC framework in PHP right now. I am trying to figure out how to deal with Ajax requests though...
A page is built using the URI www.domain.com/controller/method/params
So the URI is passed through a router class, that gets the controller and methods + params to use.
This sounds good so far, but I will also have a header and footer, maybe a sidebar even on the actual page and the the MVC will fill the main content part of the page.
When I make an Ajax request though, example to www.domain.com/user/create : controller=user method=create
I would then try to build the whole page again since it is sending a request to my app.
I am confused on how to deal with this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有一些不完整的答案。操作方法如下,非常简单...
您要做的就是检查 AJAX 标头并根据该标头提供不同的内容。 AJAX 标头为:
对于 AJAX 请求,它将设置为
XMLHttpRequest
。这是所有主要 javascript 库都遵循的标准。因此,知道这一点后,我们可以检查方法中的 AJAX 标头并相应地显示输出。
There are some incomplete answers here. Here's how you do it, it's pretty simple...
What you do is check for the AJAX header and serve different content based on that. The AJAX header is:
It will be set to
XMLHttpRequest
for AJAX requests. This is a standard followed by all the major javascript libraries.So, knowing this we can check for the AJAX header in your method and display output accordingly.
这实际上取决于您使用的框架如何执行此操作,但通常我所做的不是渲染完整的视图,而是回显出我需要的一小部分内容。
因此,如果您通过 ajax 创建用户,也许您想要返回的只是成功时包含用户 id 的整数。
因此:
因此,您对请求的字面响应将是“9”(如果这是生成的主键)
将其与正常的全页操作进行比较,在该操作中,您获取布局的每个组件,然后呈现您的特定页面的视图例如正在加载:
这通常是我处理它的方式,这不是真正的代码,但应该明白这一点。
It really depends on what framework you are working with how you do this, but normally what I do is instead of rendering a full blow view I just echo out the small bit of content I need.
So if you are creating a user via ajax, maybe all you want to return back is an integer with the user id on success.
So:
So your literal response to the request would be "9" (if that was the primary key generated)
Compare it to a normal full page operation where you to fetch each compontent of your layout and then render the view for the specific page you were loading for example:
That is normally how I handle it, this is not real code but should get the point accross.
您能具体说明您使用的是哪个框架吗?
如果您使用一些标准框架,那么框架中有一些标准方法可以使用
ajax
请求,例如cakephp
中,有一个名为ajax
的默认布局。在 Cakephp 中,我们可以使用类似 $this->autorender = false 的内容来传递一些json
字符串或xml 字符串。
Can you specify which framework you are using?
If you are using some standard framework then there are some standard methods in the framework to use
ajax
request like incakephp
there is default layout calledajax
. Incakephp
we can use something like$this->autorender = false
in order to pass somejson
string orxml
string.使用
die()
或exit()
停止之前的代码执行并渲染所有页面use
die()
orexit()
to stop former code execution and rendering all page