1

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?

3
  • Maybe I'm not understanding you, but shouldn't the act of building the page be a method (or several methods)? If so, then I don't see why user->create would have anything to do with actually building the page. I dunno, I may have missed the boat completely on this one so I'll shut up. :) Commented Sep 2, 2011 at 4:25
  • Can you please specify which MVC framework you're using???? Commented Sep 2, 2011 at 4:36
  • @Vins - he said hes making his own Commented Sep 2, 2011 at 4:55

4 Answers 4

3

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand this, but where should I be building my pages template-header-footer, etc...?
In my code you would build your views right after the if statement. An AJAX request wouldve killed the script in the if statement.
1

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.

1 Comment

Yeah, what @Aknosis said. :) (See my comment on the question for context) +1
0

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.

Comments

0

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

1 Comment

Just a little FYI: exit is a construct so you don't have to include the parenthesis: die; or exit; will both work. If you provide a parameter to exit it will be the exit status of the script (if running on the command line) and a paramter to die will echo it as output just before exiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.