RIA development: Coexistence of two MVC frameworks (client<->server)
we are trying to build a RIA for a university project using two MVC based frameworks; JS (Ext JS 4) on the client side and PHP (presumably YII) on the ser开发者_开发知识库ver side.
While we are in the early stages learning Ext JS our tutor is encouraging us to see the big picture figuring out how could we make the two MVC frameworks coexist and work together.
Right now I'm programing some test applications where the JS framework is calling different .php's, one per task, and im starting to figure that's not the way to go.
I was thinking using the following scheme; Server side acting as a API. Connecting both systems through two views. Client side making calls to the server side from a client view to server objects and their members using e.g. json's to a unique .php file that would act as a server view, those calls would be processed in the server doing the stuff internally in a MVC fashion and returning the appropiate data to the client.
So... since i don't have experience dealing with MVC architectures yet I have no idea if what I'm saying makes any sense.
I guess there must be a right way to make this work. Any suggestions or pointers please?
Thanks in advance.
Actually I'm on a project like yours and i'm using zend framework for the server side and not yii but principles are the same.in your server-side`s view you will need to setup loading of extjs js files.This is my php code for it:
<?php
// Initialize Extjs on development ENV
if (APPLICATION_ENV == 'development') {
$this->headLink ()->prependStylesheet ( './javascript/extjs/resources/css/ext- all- debug.css' );
$this->headScript ()->prependFile ( './javascript/extjs/ext-debug.js' );
} // Initialize Extjs on production ENV
else {
$this->headLink ()->appendStylesheet ( './javascript/extjs/resources/css/ext- all.css' );
$this->headScript ()->prependFile ( './javascript/extjs/ext.js' );
}
// Load EXTJS MVC Startup from the public folder
$this->headScript ()->appendFile ( './app.js' );
// Output aggregated css & js files
echo $this->headLink();
echo $this->headScript();
?>
I have created one controller(php file)on server-side per every form/grid,etc i create on client side and they will interact with each other easily and using JSON.For example you create a form on extjs and your forms submit url would be the path to your server side controller and you code process them using $_POST[fields];
You should disable view for controllers which send json data.
Understanding MVC takes time and practice. What i can suggest you is to first choose your desired php framework,study it
s MVC and then easily add extjs into it.
Your thinking is close, but you're selling the "Controller" part of MVC short in that architecture.
You're right that you'll be making calls from a client view to server objects. But that call doesn't have to be made to different php files. Yii has support for routing, which means that you can call different methods from the same controller. Say for instance your controller is responsible for basic CRUD operations (Create, Read, Update, Delete, in case you're unfamiliar). You'd have 1 method for each operation in one controller. Your api may look like this:
- http://mysite.com/ria/add
- http://mysite.com/ria/get
- http://mysite.com/ria/edit
- http://mysite.com/ria/delete
Note that all of these calls are going to the RiaController
. This should be handled by Yii's routing engine.
Returning the data in a view would be a little more work than is necessary. Since you're using javascript to make the request, it would be easier if that javascript received JSON or XML instead of an HTML view. Yii does have a JSON class that is capable of turning PHP data into JSON. I recommend looking into that.
At the end of each of your methods in your RIAController, you're responsibility at that point would be to return the output from CJSON (e.g. $this->renderText(CJSON::encode($myoutput));
). It's this output you'll use on your client side to do your work.
精彩评论