如何在 PHP 中使用 MVC 处理 Ajax 请求

发布于 2024-12-02 08:38:55 字数 384 浏览 5 评论 0原文

我现在正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窗影残 2024-12-09 08:38:55

这里有一些不完整的答案。操作方法如下,非常简单...

您要做的就是检查 AJAX 标头并根据该标头提供不同的内容。 AJAX 标头为:

$_SERVER['HTTP_X_REQUESTED_WITH']

对于 AJAX 请求,它将设置为 XMLHttpRequest。这是所有主要 javascript 库都遵循的标准

当我发出 Ajax 请求时,例如
www.domain.com/user/create :controller=user method=create

因此,知道这一点后,我们可以检查方法中的 AJAX 标头并相应地显示输出。

function create() {

    // code to create user

    if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest') {
        // return new user id in json format (xml)
        die( json_encode( array( 'user_id' => $new_user_id ) ) );
    }

    // load views hear
}

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:

$_SERVER['HTTP_X_REQUESTED_WITH']

It will be set to XMLHttpRequest for AJAX requests. This is a standard followed by all the major javascript libraries.

When I make an Ajax request though, example to
www.domain.com/user/create : controller=user method=create

So, knowing this we can check for the AJAX header in your method and display output accordingly.

function create() {

    // code to create user

    if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest') {
        // return new user id in json format (xml)
        die( json_encode( array( 'user_id' => $new_user_id ) ) );
    }

    // load views hear
}
落花随流水 2024-12-09 08:38:55

这实际上取决于您使用的框架如何执行此操作,但通常我所做的不是渲染完整的视图,而是回显出我需要的一小部分内容。

因此,如果您通过 ajax 创建用户,也许您想要返回的只是成功时包含用户 id 的整数。

因此:

class User {
 public function create(){
    $uid = //create user in database return primary key
    echo $uid;
    exit; //we stop execution here so we don't render the full layout
 }
}

因此,您对请求的字面响应将是“9”(如果这是生成的主键)

将其与正常的全页操作进行比较,在该操作中,您获取布局的每个组件,然后呈现您的特定页面的视图例如正在加载:

class Home {
public function index(){
    $homeView = //loadHomeView
 return $homeView
}

这通常是我处理它的方式,这不是真正的代码,但应该明白这一点。

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:

class User {
 public function create(){
    $uid = //create user in database return primary key
    echo $uid;
    exit; //we stop execution here so we don't render the full layout
 }
}

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:

class Home {
public function index(){
    $homeView = //loadHomeView
 return $homeView
}

That is normally how I handle it, this is not real code but should get the point accross.

沐歌 2024-12-09 08:38:55

您能具体说明您使用的是哪个框架吗?

如果您使用一些标准框架,那么框架中有一些标准方法可以使用 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 in cakephp there is default layout called ajax. In cakephp we can use something like $this->autorender = false in order to pass some json string or xml string.

烟柳画桥 2024-12-09 08:38:55

使用 die()exit() 停止之前的代码执行并渲染所有页面

use die() or exit() to stop former code execution and rendering all page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文