PDA

View Full Version : ExtJS and ZendFramework... the way i did it



predator
28 May 2010, 1:55 PM
Here I will outline the integration process between ext and zf, I would really want to know if you think that some of my approaches are wrong or they can be done in a better way.

Also I know that I will talk mainly about php here, so if this is supposed to go in a different category really sorry.

Now the first obvious problem is that ZF is MVC framework, but with extjs the View part is not really needed and needs to be taken away somehow. There are two ways to do that:

first one is to disable the view in each controller by placing this code in the init() method


public function init()
{
$this->getHelper('layout')->disableLayout();
}
The second option is to create a plugin with predispatch method that will take care of it globaly
I found it very useful to merge it together with my user authorization stuff
In my access check plugin i have the following preDispatch

public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if($this->_auth->hasIdentity()) {
//User is logged in
// All server calls are ajax/json, so we dont need the zend view
if($request->controller == 'auth') {
// dont disable it for auth controller
} else {
// Disable Layout
$layout = Zend_Layout::getMvcInstance();
$layout->disableLayout();
// Stops ZF from looking for scritps/*.phtml
$fc = Zend_Controller_Front::getInstance();
$fc->setParam('noViewRenderer', true);

}
// This user has no identity and needs to see the login page
} else {..........
Note that this plugin needs to be registered in the bootstrap.php, create a method that starts with _init*.() and register the plugin

protected function _initAuthorization()
{
/**
* This will check only if user is logged in
* Specific permissions MUST be checked within
* the appropriate controller, action or method
*/
$auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($auth));
}Now you can load your js files within the layout file and not being bothered about any views from zf. Note that you can still benefit from everything else that ZF has to offer. But in a way you are turning it into json server that will handle requests and serve results.

Now here is my other problem... once the user is logged in and so on... the js files are served and the interface is loaded... but lets say the session on the server side expires.. the UI stills stays loaded and the only way for the user to see the login screen again is to refresh the page or revisit it... I needed some time to figure out a solution for that. Here is my approach
in my AccessCheck plugin, the same I use for disabling the layout I have the following code to be sent in a case of expired session


header('HTTP/1.1 401 Unauthorized'); // ExtJS is listening for this code in the header.
and in my layout file the following extjs code is handling 401 header

Ext.Ajax.on('requestexception', function(conn, response, options) {
switch (response.status) {
case 401:
Ext.getCmp('mainViewport').destroy();
Ext.Msg.show({
title:'<div align="center">Session Expired!</div>'
,msg:"<center>Your session has expired or you have logged out from another place.<br>Click OK to login again.</center>"
,height:200
,width:440
,closable:false
,buttons:Ext.Msg.OK
,fn: function(btn){
if(btn == 'ok') {
window.location = GV.baseUrl()+'/auth/login';
}
}
});
}
});
I really wonder if somebody else is working with ZF as backend how is he handling those two problems.
Thanks for reading

joeri
29 May 2010, 2:08 AM
We're also shipping ExtJS on top of zend framework. We ended up going a different route. We're not using any of the ZF MVC classes. We just implement all server-side functionality as JSON-RPC services (zend_json_server). This way we can design a clean server-side API, which helps for security and for third-party app integration (third-party code can call the same API that our own UI does).

For user sessions, we do something similar to what you do, and we also send keep alive messages.

What is it in the MVC classes that you find useful even without the view portion?

predator
29 May 2010, 11:26 AM
For my type of project I don't really need a framework for my backend, but i tought that it will be useful for me to try and learn one. I chose ZF because it is more like a collection of classes and doesn't really restrain you in some special structure and rules. However as I like the freedom my point was to learn the OOP and MVC practices somehow, so I try to setup and use the framework by the book and just exclude the view part as this is handled separately by Ext. Some of the benefits i get is that I have basically everything besides the view handled by ZF in a native way.. database connection, authorization, sessions, module auto loading and etc

My idea was to keep and use the framework the same way as i would do it without Ext being the front end. Maybe is a bad approach..

Now I am thinking to compile own small framework... something based on CI probably and focus it only on Ext needs. I would like it to have somekind of dynamic build and minification of the ext code... to include only those classes that are used or depended by the application... still wondering is it actually possible...

joeri
29 May 2010, 11:34 AM
It's very easy to pick the parts of ZF you want, which is why we ended up using it. The module auto-loading is something that you can use independently from the MVC classes without effort. Same for the database connection. I don't have experience with authorization and sessions, because we rolled our own code for that (for reasons of legacy), but I suspect ZF's modules for that are easy to use independently.

I would recommend avoiding rolling all your own code. ZF is built and maintained for free. It is always going to take you less time and effort to reuse parts of ZF than to build alternatives for those yourself.