如何将 AJAX 集成到 MVC 风格的 Web 应用程序中?
我正在使用一个非常简单(可能并不真正符合 MVC)的 MVC 框架开发一个 Web 应用程序,在开发应用程序时由我自己编写代码以保持代码整洁。 不过,我的应用程序有许多 AJAX 组件,现在我一直在尝试将它们集成到通用 MVC 结构中。它们应该如何整合?
我的 Javascript 文件中有类似这样的内容:
$('#pageList').load(BASE_SITE_URL + 'ajax/pageList.php');
并且 pageList.php 曾经具有如下所示的结构:
<?php
require '../includes/config.inc.php';
require BASE_PATH . 'includes/init.inc.php';
// a whole load of Controller logic here and then...
echo "<table>";
//display some user data
echo "</table>";
我对此真的很困惑,任何建议都会受到赞赏
I'm developing a web application using a very simple (maybe not really MVC-compliant) MVC framework, coded by myself while developing the application to keep the code clean.
My application, though, has many AJAX components and now I'm stuck trying to integrate them within the general MVC structure. How should they be integrated?
I have something like this in my Javascript files:
$('#pageList').load(BASE_SITE_URL + 'ajax/pageList.php');
and pageList.php used to have a structure like the following one:
<?php
require '../includes/config.inc.php';
require BASE_PATH . 'includes/init.inc.php';
// a whole load of Controller logic here and then...
echo "<table>";
//display some user data
echo "</table>";
I'm really confused about this, any advice is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几点建议 - 您的网站应该在没有 JavaScript 的情况下正常运行。例如,如果您有像 /list/?page=1 ... /list/?page=n 这样的页面 url 分页,那么您应该确保所有页面都可以点击,而实际上不需要 javascript。
Javascript 确实应该是您网站的扩展。在上面的例子中,你可以回来使用js用简单的ajax行为来替换所有分页的功能。您可能想要做的是使用 jquery.load 执行以下操作:
注意加载后的选择器。这非常重要,因为我实际上还没有创建任何新的 HTML 页面来使我的网站启用 AJAX。相反,我使用浏览器已有的内容作为简单的 URL,并使用 ajax 加载它。
当然,有时您只需要 AJAX 内容,但 URL 上并不真正存在。在这种情况下,我建议为所有 ajax 内容创建一个控制器,然后用视图渲染每个路径。
我希望这有帮助。
Few pointers - your website should be functional without javascript. For example, if you have pagination with page urls like /list/?page=1 ... /list/?page=n then you should make sure all of your pages are clickable without actually needing javascript.
Javascript should really be an extension to your website. In the above example, you can come back and use js to replace the functionality of all the pagination with a simple ajax behavior. What you would probably want to do is use jquery.load to do something like:
Note the selector after the load. This is very important because I haven't actually created any new HTML pages to make my website AJAX enabled. Instead I use what already is available to the browser as a simple URL and load it using ajax.
Of course, there are times that you need AJAX only content where won't really exist on a URL. In a situation like this I recommend creating a controller for all your ajax stuff and then rendered each path with a view.
I hope this helps.
以下是我在应用程序中执行的操作:
检查名为
XMLHttpRequest
的$_SERVER
变量。如果已设置,则它是一个 ajax 请求,然后您可以执行一些逻辑,以便您的代码不会加载视图(模板等)并输出不同的header()
(如果有)。 。Here's what I do in my apps:
check for a
$_SERVER
variable calledXMLHttpRequest
. If it's set, it's an ajax request and then you can do some logic so that your code doesn't load the views (templates and what not) and outputs a differentheader()
(if any)..我通常有一个与每个视图相关的 js 脚本,称为 update.js。它的作用是定义用户如何与视图交互并创建 HTTPREQUEST。
然后我有一个名为controller.js的文件来接收它,决定如何处理这个请求
并向模型询问响应。
最后,属于视图的 update.js 使用从模型接收到的新信息更新视图。
我
按其
而
类型
view.phpupdate.jsindex.php
文件。我认为这是使用 AJAX 实现 MVC 模式的最佳方法。请记住,此模式的目标是保持封装性和可扩展性,这对我来说是最好的方法。
I usually have a js script related to each view called update.js. It's role is to define how the user interacts with the view and create the HTTPREQUEST.
Then I have a file called controller.js that receives it, decides what to do with this request
and asks the model for the response.
Finally the update.js which belongs to the view updates the view with the new information received from the model.
My app directory structure is like this:
model/
controller/
view/
index.php
I organize the files by it's role rather than the type. I think this is the best approach to the MVC pattern with AJAX. Remember that the aim of this pattern is to keep encapsulation and scalability and this is the best way to do it for me.