简体   繁体   中英

How to handle Ajax request with MVC in PHP

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?

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
}

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.

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.

使用die()exit()来停止以前的代码执行并呈现所有页面

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM