Rails takes the approach of responding to different types of requests by mime/type (I believe the Prototype javascript library sends special HTTP header in its AJAX requests). Most controller code should be the same no matter how its being invoked, its just the response that varies. Here's how Rails does it:
Code:
def update
@record = SomeModel.find(params[:id])
@record.update(params[:record])
respond_to do |wants|
wants.html { # render some html }
wants.js { # return some response to an ajax request, perhaps just some data or a JSON encoded string }
wants.xml { # send back some XML with the appropirate mime type }
wants.whatever { # custom responds types }
end
end
Now PHP doesn't have blocks that makes the above code so succinct but I'm sure something similar could be accomplished with PHP.
Bookmarks