4

I'm new to the OOP paradigm (and AJAX/jQuery), but would like to create a basic site employing MVC architecture, in PHP, with AJAX functionality. I drew up a brief diagram of how I currently 'understand' the architecture.

Presumably when AJAX is used, that acts as the controller to interact with the model directly to retrieve whatever functionality is needed? The filenames I added are just to give you an idea of what I 'think' should be included. e.g. index.php would be a html/css template with includes to modules in the relevant places (whatever they may be) - news.php, navigation.php, etc. database.php/pager.php might house the classes and extended classes that I create for pagination, or connecting/querying the database I'm struggling to see what the controller component could be - it'd surely end up being a 'second back-end view' - calling the classes from the model to be sent to the view?

I've probably portayed my confusion well here - what should go in the view, controller and model... is AJAX functionality technically another controller? Any diagram similar to my one above would be extremely helpful.

6
  • 1
    MVC is not an architecture. It's a design pattern. Commented Sep 17, 2011 at 11:05
  • My mistake, care to explain the difference? Commented Sep 17, 2011 at 11:10
  • 2
    MVC is an architectural design pattern. Commented Sep 17, 2011 at 11:14
  • @Chris see secure.wikimedia.org/wikipedia/en/wiki/Software_architecture. MVC is just a web-presentational pattern. Also see devzone.zend.com/article/12997 Commented Sep 17, 2011 at 11:16
  • @Gordon so you mean that MVC does not affect your architecture at all? Commented Sep 17, 2011 at 11:41

3 Answers 3

8

OK so AJAX is a transport method and not a piece of application like a model or controller.

Your client will communicate through AJAX with one or more Controllers.

These Controllers use or invoke Models to handle different kind of tasks.

Then either the controller or the model responds to the request either with a message in a transport-friendly format (JSON, YAML, XML) or with a View (piece of HTML).

The controller handles requests, that means it receives the initial client-input. Depending on the situation this input has to be formatted, normalized, mutated or transformed somehow before being used in your application.

Then a controller uses or invokes a model; this means that it either deals with business logic itself (old style) and makes use of the model to access datasources or it hands the main workflow of your application completely over to the model (new style).

A model in first instance abstracts a persistent storage entity (like a database). In contemporary application design it also does the main business logic of your application.

Sign up to request clarification or add additional context in comments.

3 Comments

So, just to make sure I understand (or at least what I think I understand)... Say I had a 'directory' site, and somebody clicked on a category link (in the view). The controller would then receive that request, then invoke the model necessary to grab information from the database, another model to sort that data into X order, another model to remove X rows (and whatever else). The controller would then send that information back to a category view template? (Forget about AJAX for the moment). I feel another diagram coming on... It's difficult for me to explain my understanding further without
giving a folder structure and code examples... which I might just do. I'm struggling to picture how everything would integrate - I think I understand the pattern, just not how to implement it.
You don't need that many models. One Model called "Category" would have methods like "create" (to make a new category), "get_subcategories" (with an order argument maybe). Then you'd have another one called "Posts" where you can "create", "delete", "get_view_counter", "update_view_counter", etc... It's a rough 1:1 relationship with the tables in your DB.
0

There's one way to see this.

  • Ajax is the medium for sending data between MVC components like HTTP POST. In this respect it does not show up in the MVC pattern.
  • The actual display in JSON format can also be seen as a view if it's actually used to show data.

From this you should be able to come to your own conclusions.

Comments

-5

You can use PHP's best MVC architecture called "YII".Get more info from here http://www.yiiframework.com/

Comments

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.