Integrating CodeIgniter with AMFPHP
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!
| Print article | This entry was posted by Danny Kopping on November 2, 2008 at 11:09 pm, and is filed under Flex, Lessons. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Thanks for the tutorial. How would you go about including CI as a library if you are also using CI as the framework for your main application. I’d like to use the same CI system and library files that run the app but i’m having trouble including it in the CodeIgniter service in your tutorial
if (!defined('BASEPATH')) define ("BASEPATH", "../../../system/");
if (!defined('APPPATH')) define ("APPPATH", "../../..//system/application/");
Any ideas are appreciated, thanks.
about 1 year ago
if (!defined(‘BASEPATH’)) define (“BASEPATH”, “../../../system/”);
if (!defined(‘APPPATH’)) define (“APPPATH”, “../../..//system/application/”);
about 1 year ago
Ok so just clarify your question for me… You posted two comments. The second one — was that what you have at the moment or is that what you’re suggesting you do?
I use the exact same methodology i have presented in this tutorial on every Flex + PHP project i use, so i’m a little confused as to what the problem you’re having is…
about 1 year ago
The second is the code that should be accessing the base codeigniter files from where I have located my CodeIgniter.php per your example above (but it’s not working). Do you see any issue with including CI as a library with amfphp from within a CI application?
about 1 year ago
Well, first off, i can see a syntax error in this code:
if (!defined(’APPPATH’)) define (”APPPATH”, “../../..//system/application/”);
…there’s an extra forward-slash in the APPPATH definition.
Have you tried changing it to this:
if (!defined(’APPPATH’)) define (”APPPATH”, “../../../system/application/”);
about 1 year ago
Also, what you can try doing is using the “realpath” function of PHP to ensure that the path to which you are pointing (ie the directory of your CodeIgniter files) actually exists and is valid.
Make a test PHP file in the same directory as where you’ve saved the code you’re having trouble with, and simply put this:
If it prints out a directory, your path is correct and we need to look further at this issue, and if it prints “false”, then your path is wrong.
about 1 year ago
That worked. Thanks!
about 1 year ago
Wicked! Good luck with it… I’d like to see how you use it once you’ve used it in a project. I’ll be doing a few more tutorials on some CodeIgniter tricks with AMFPHP and Flex soon. Just got to find the time…
about 1 year ago
Hey guys, just fyi i got CI to work with AMF without needing any hacky stuff: http://codeigniter.com/wiki/AMF_Codeigniter_Library_No_Zend_No_Hooks_Etc./
about 11 months ago
Well I have Adam’s problem
How can I call CI controller using AMFPHP
I used TestService as extends myCIController
But libraries used in myCIController or myCIModel don’t work (exp: validation: this->validation->set_rules() gives ERROR Call to a member function set_rules() on a non-object — or this->db->select() gives ERROR No )
Plz help
about 11 months ago
Hi Darin
Your question is a little vague.
“I used TestService as extends myCIController”
Are you using the exact mechanism that i described in the tutorial or have you customized it?
about 11 months ago
I am developing website in codeigniter. I need to use a newsletter module for displaying on the website and also sending it to the subscribers. I am using phplist for this. Can you please help me how to integrate it with CI.
I also want to integrate the Google Map API. How to get ti done as well
about 8 months ago
Every one, tell me how can I use the model class of CodeIgniter integrated with AMFPHP. Because I have the files configured as before way, but when I try to call or load any class or model created previusly, appear one error. Tell me one way to use this.
about 7 months ago
So,you mean,if i do follow your tutorial,i must use codeigniter as a library but not a framework.Why don’t you use ci as a framework? If i want to use CI as a framework, how could i do?Thanks.
about 7 months ago
Hi Chris, thanks for the comment
CodeIgniter is essentially an MVC framework for PHP. CodeIgniter was originally meant to output to HTML, so if you want to use it with Flex, you are pretty-much not leveraging the framework in full. You can’t use the full CodeIgniter framework in Flex because a lot of your logic will be implemented in ActionScript, so i thought that – since CodeIgniter is great for getting a site together quickly – it would be nice to use it as a library of PHP code on the server-side.
If you need more clarity, feel free to ask away!
about 3 weeks ago
Thanks for the article..I am new to php and found this more informative.I had read ur tutorial but when i run testservice on wamp server its saying waiting for localhost and i am not able to see output.Can anyone help me…
Thanks,
Harsha.
about 11 months ago
Hi Visi
Unfortunately, i haven’t used CodeIgniter as it was intended… I merely use it as a library, not a framework. Can’t really help you there… try checking out the CodeIgniter website’s forums for some help.
Thanks