28 Feb

Killer Applications with CakePHP, JQuery and Adobe Air

CakePHP, Latest Developments, jQuery

Have you ever wished to program desktop applications for your own web app? If you are like me, that is, most of the lazy web programmers… you never put your hands on any desktop language. Even though all of us knows what flexibility can be given to a web app if we develop its desktop based client.

I came across a new development framework, Adobe AIR, which lets you build cool desktop based application simply with web technologies. If you are good at HTML and AJAX you can easily start with this new framework. I was surprised to see that there are so many applications already using this. After just 3 hrs of experimentation with this, I started loving this new tool. It is very cool. Some advantages which impressed me are:

  • Real Fast: I built a stock market monitoring tool in just 3 hrs. It didn't look like I had to learn something new, everything went smooth. It was like developing AJAX enabled HTML pages and testing them in browser. Once you're done, you simply create xml configuration files, copy a JS from framework, compile it, and you're done.
  • Use your favorite JS Library: This is an amazing feature. My personal favorite is JQuery and I use it in almost every project. Please note: you have to have the latest version of JQuery to get it working with Adobe AIR. They recently patched for Adobe AIR only. I already wasted 30 minutes in solving security issues.
  • Cool CSS Extensions: Check this out.
  • Easy Debugging: Any errors/exceptions appear in command window.
  • Easy Drag and Drop: Super-easy functions like dragstart, drag, dragend, dragenter, dragover, dragleave, and drop.

Check out my stock market tool, just a basic version:

Everything is linked to a backend application which runs on CakePHP. Login function (UsersController::login) returns session_key to the desktop application if login is successful.

JavaScript:
  1. function process_login()<br />
  2. {<br />
  3. $("#status").html("Logging in...");<br />
  4. user=$('#user').attr('value');<br />
  5. pass=$('#pass').attr('value');<br />
  6. $.get("http://localhost/StockBack/users/login/"+user+"/"+pass, function(session_key){<br />
  7. if(session_key=='false')<br />
  8. {<br />
  9. $("#status").html("Unable to login");<br />
  10. }<br />
  11. else<br />
  12. {<br />
  13. $("#status").html("Logged in successfully");<br />
  14. $("#login").hide("slow");<br />
  15. $("#panel").show("slow");<br />
  16. }<br />
  17. });<br />
  18. }

(Sorry for posting code like this but my code editor in wordpress sucks – if anyone knows a good one, please recommend me)

To start learning, some important links are:

Developing Adobe AIR Applications with HTML and Ajax

Adobe AIR Quick Starts for HTML

Adobe AIR Language Reference for HTML Developers

The Adobe AIR HTML documentation set (a ZIP file) is available for download here:

http://www.adobe.com/go/learn_air_html_docs

09 Jan

CakePHP Vendors: What are they and why are they used ?

CakePHP

As in my previous post about creating an administrator panel in CakePHP, my another programmer needed my help in telling him more about vendors.

This was his questions:

What are vendors and why are they used ?

Vendors are used in CakePHP to use external PHP classes/libraries. Lets say you downloaded a class from some great classed database like phpclasses.org or you had your own class already developed, and want to use it in you Cake application. You can use vendor for it. Its just like include() in PHP, however its a bit different. The vendors folder is where you’ll place third-party PHP libraries you need to use with your CakePHP applications.
For example:

Lets say we have a file my_library.php, we'll put it in /app/vendors.
my_library.php is as follows:

PHP:
  1. class Area
  2. {
  3. function calculate_area($length, $width)
  4. {
  5. return $length*$width;
  6. }
  7. }

In order to use this class inside my Cake app, I'll have to tell Cake about that. To use this in a controller, I can write following code in my controller, lets say my controller is items_controller.php, and it contains a function called show_area():

PHP:
  1. function show_area($length, $width)
  2. {
  3. vendor('my_library'); //this will just include my_library.php at this stage
  4. $area=&new Area();
  5. $current_area=$area->calculate_area($length, $width);
  6. $this->set('area', $current_area);
  7. }

Using this way, we can use 3rd party classes inside our controllers / models / view and also in our components / behaviors / helpers.
Many people ask this question:

Why are there two “vendors” folders in CakePHP?

Here's the answer for them

- Abhimanyu Grover

14 Dec

CakePHP Tutorial - How to create an administrator panel in CakePHP

Articles, CakePHP

My new programmer wanted to know how to create administrator panel using CakePHP, so I thought to share my written tutorial with the community. Here it goes:

1. First, lets tell CakePHP that we need an admin panel to use for this app. Open config/core.php

PHP:
  1. define('CAKE_ADMIN', 'admin');   // uncomment this line if its commented already

2. Now all the controller functions prefixed with "admin_" are admin functions and will be accessible with URL's such as:

PHP:
  1. /admin/posts/add
  2. This URL will trigger Posts::admin_add()

You can define multiple function prefixed with 'admin_'. Remember all the views have to be with the same name i.e. admin_add.thtml

3. Now lets create a new layout for the admin in /app/views/layouts directory. Lets call it admin.thtml

PHP:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <?php echo $html->css('cake.generic');?>
  5. </head>
  6. <body>
  7. <div id="content"><? echo $content_for_layout; ?></div>
  8. </div>
  9.  
  10. <?php echo $cakeDebug?>
  11. </body>
  12. </html>

4. Now we have the URL's for admin, we also have the controller functions for admin. Now we need to create a functionality which will automatically use the above layout for all of our admin functions. To do this, create a file app_controller.php in 'app' directory. Or you can also copy it from /cake.

PHP:
  1. <?php
  2. class AppController extends controller
  3. {
  4.     // this is a cool function which comes useful in many cases
  5.     function beforeFilter()
  6.     {
  7.         // if its the administrator/manager - change the layout
  8.         $pos = strpos($_SERVER['REQUEST_URI'], CAKE_ADMIN);
  9.         if($pos == true)
  10.         {
  11.             $this->layout='admin';
  12.         }
  13.     }
  14. }
  15. ?>

AppController::beforeFilter() function will assign admin.thtml immediately by checking the URL being called, and this will be reflected in all controllers of your application. Now you can access URL's like /admin/posts/add, /admin/posts/edit/1 or similar, but not /admin alone which carries out authorization.

That's all you need to do. Next step is adding a simple authorization for administrators, which I'll explain in my other tutorial.

- Abhimanyu Grover