Posts tagged codeigniter
Handling multiple remote services with RemoteObject – the easy way!
May 6th
Ok, so you’re developing a Flex application with a bit of server-side integration (using PHP, Java, Ruby, .NET, etc) and your application is getting a little intense; multiple service calls to the server, custom logic for success and failure of said calls and everything in between.
You could use the following syntax for handling mutliple method-calls from a remote script (in this example MyService):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <mx:RemoteObject id="service" destination="amfphp" source="MyService" makeObjectsBindable="true"> <mx:method name="doSomething" result="doSomethingResult(event)" fault="doSomethingFault(event)"/> <mx:method name="doAnotherSomething" result="doAnotherSomethingResult(event)" fault="doAnotherSomethingFault(event)"/> </mx:RemoteObject> <mx:Script> <![CDATA[ private function doSomethingResult(event:ResultEvent):void { // custom logic } private function doSomethingFault(event:FaultEvent):void { // custom logic } private function doAnotherSomethingResult(event:ResultEvent):void { // custom logic } private function doAnotherSomethingFault(event:FaultEvent):void { // custom logic } ]]> </mx:Script> |
I dunno about you, but to have to define special tags in MXML for each bloody method in your script is too much effort (and can leave you with some nasty/tricky bugs if you forget to add a new tag after adding a new method in your server-side script!). I used the following mechanism in my post about integrating CodeIgniter, Flex and PHP if i remember correctly… Give me a break… I was too lazy to check (it’s midnight and i’m sleepless).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <mx:RemoteObject id="service" destination="amfphp" source="MyService" makeObjectsBindable="true" result="resultHandler(event)" fault="faultHandler(event)"/> <mx:Script> <![CDATA[ private function resultHandler(event:ResultEvent):void { var message:RemotingMessage = event.token.message as RemotingMessage; switch(message.operation) { case "doSomething": // custom logic break; case "doAnotherSomething": // custom logic break; default: trace("Shit... Missed this one... " + message.operation); break; } } private function faultHandler(event:FaultEvent):void { var message:RemotingMessage = event.token.message as RemotingMessage; switch(message.operation) { case "doSomething": // custom logic break; case "doAnotherSomething": // custom logic break; default: trace("Shit... Missed this one... " + message.operation); break; } } ]]> </mx:Script> |
Seems much easier, doesn’t it? Essentially all you’re doing is examining the event parameter of the ResultEvent or FaultEvent, finding which method was just called and acting accordingly… This will work with any number of method calls, from any number of different RemoteObjects. You can point them all to the same handlers!
If you guys have any alternative ways of doing this, let me know!
Using PHP to Backup and Save your MySQL Database
Apr 17th
While working on a recent project, i needed to set up a cron job that would run every X amount of hours and backup a database. I’ve done this a few times before, but only this time (while Googling for a better solution) i found David Walsh‘s blog post on how to Backup Your MySQL Database Using PHP. David’s done a great job and i definitely recommend you check his blog out in its entireity for some really neat web dev articles.
I decided to go and put David’s script into class form (my case of OCD is genuine
) and i added one or two things to it. Basically it allows you to backup one or many tables in a database, include or exclude the data from the backup and i also included Rick Ellis’ (of CodeIgniter fame) Zip class to allow you to zip up the backup and stash it somewhere.
Thanks to David Walsh and Rick Ellis!
Here’s the class: DatabaseDump.php
…and here’s how to use it:
$dump = new DatabaseDump("host", "user", "password", "database", "destination/"); $dump->backup();
Be sure to check out the DatabaseDump.php class for the documentation!
Integrating Flex, AMFPHP and CodeIgniter (also including Value-Objects)
Jan 5th
Integrating CodeIgniter with AMFPHP
Nov 2nd
A while back i spent an evening hacking away at the CodeIgniter source code. I was desperate to use it as a library, as opposed to a framework. I don’t use PHP with HTML. I did for a little bit, but personally i found that HTML and CSS were the biggest load of shit languages ever to plague my existence! Sorry. Once i was introduced to Flash and Flex, i said “to hell with HTML and it’s ugly sister CSS! I’m becoming a banner-maker!”. Not quite… i’ve never made a banner – even though that’s like totally the whole point of Flash, dude… (insert stupid programmer comments here).
Ahem. Back to the point. So when i started writing Flex apps and using AMFPHP for my remoting, i desperately wanted a nice concise, easy-to-use, well documented library. Enter CodeIgniter by Rick Ellis. Although CI is actually a MVC framework for PHP, i decided i was going to turn it into a library! (and what a ballache that was).
Below is the massive, complex, obfuscated script I wrote to turn it into a library. Notice how the script seems to drip with programming excellence:
<?php if (!defined('BASEPATH')) define ("BASEPATH", "../CodeIgniter/system/"); if (!defined('APPPATH')) define ("APPPATH", "../CodeIgniter/system/application/"); if (!defined ("EXT")) define ("EXT", ".php"); require_once (BASEPATH."codeigniter/Base5.php"); require_once (BASEPATH."libraries/Controller.php"); require_once (BASEPATH."codeigniter/Common.php"); ?>
Crazy huh? Haha. Essentially, all this script does is fuck with some of the framework architecture and allow the package to be required in PHP. See the usage below:
<?php
require_once("CodeIgniter.php"); // the path to your CodeIgniter.php script as described above
class AMFPHP_CI_Integration { function __construct() // PHP constructor { parent::Controller(); // Extend the functionality of the CI Controller class } }
?>
And that’s it! Flimsy and hackerish, i know, but hey… It works
Any queries or suggestions are most welcome!