Tuesday, August 25, 2009

Using Zend Framework with CodeIgniter

If you ever wanted to integrate CodeIgniter and Zend Framework, you might have come across this tutorial by Daniel Vecchiato.

Whilst Daniel has done a great job demonstrating the possibility of using the two frameworks together, concerns have been made: do we actually need to use hooks?

As I understand it, hooks are used to extend the core functionalities of CodeIgniter (as explained in the user guide). Obviously Zend Framework and CodeIgniter are two different systems and there is no intention for us to extend CodeIgniter’s core functionality with Zend Framework.

Using hooks can be dangerous as it’s system-wide, and it modifies the system behaviour.

What I have done is to simply use CodeIgniter’s library structure to load the Zend Framework resources. Below is the tutorial.

Assuming you already have CodeIgniter installed. If not please refer to the user guide for installation.

1. Download Zend Framework from the official website.
2. Unzip the Zend Framework package, and copy the Zend folder (under Library) to your CodeIgniter installation’s application/libraries/. You can actually place the folder anywhere, but remember to alter the script accordingly (read the comments in the script!).
3. Place the library script (provided at the end of the post) in application/libraries/
4. Done! That’s all you need to do. Now, let us see an example of using the library.

Usage Sample


01.02.
03.class Welcome extends Controller {
04.
05. function Welcome()
06. {
07. parent::Controller();
08. }
09.
10. function index()
11. {
12. $this->load->library('zend', 'Zend/Service/Flickr');
13. // newer versions of CodeIgniter have updated its loader API slightly,
14. // we can no longer pass parameters to our library constructors
15. // therefore, we should load the library like this:
16. // $this->load->library('zend');
17. // $this->zend->load('Zend/Service/Flickr');
18.
19. $flickr = new Zend_Service_Flickr('YOUR_FLICKR_API_KEY');
20.
21. $results = $flickr->tagSearch('php');
22. foreach ($results as $result)
23. {
24. echo $result->title . '
';
25. }
26. //$this->load->view('welcome_message');
27. }
28.}
29.?>

Library Script

Copy the code and paste it to a new file called Zend.php in application/libraries/.

01.02.
03./**
04. * Zend Framework Loader
05. *
06. * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
07. * in CI installation's 'application/libraries' folder
08. * You can put it elsewhere but remember to alter the script accordingly
09. *
10. * Usage:
11. * 1) $this->load->library('zend', 'Zend/Package/Name');
12. * or
13. * 2) $this->load->library('zend');
14. * then $this->zend->load('Zend/Package/Name');
15. *
16. * * the second usage is useful for autoloading the Zend Framework library
17. * * Zend/Package/Name does not need the '.php' at the end
18. */
19.class CI_Zend
20.{
21. /**
22. * Constructor
23. *
24. * @param string $class class name
25. */
26. function __construct($class = NULL)
27. {
28. // include path for Zend Framework
29. // alter it accordingly if you have put the 'Zend' folder elsewhere
30. ini_set('include_path',
31. ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
32.
33. if ($class)
34. {
35. require_once (string) $class . EXT;
36. log_message('debug', "Zend Class $class Loaded");
37. }
38. else
39. {
40. log_message('debug', "Zend Class Initialized");
41. }
42. }
43.
44. /**
45. * Zend Class Loader
46. *
47. * @param string $class class name
48. */
49. function load($class)
50. {
51. require_once (string) $class . EXT;
52. log_message('debug', "Zend Class $class Loaded");
53. }
54.}
55.
56.?>


Happy coding! Oh and don’t forget, Zend Framework is PHP 5 only, so it won’t work on your PHP 4 installation.
Update: Using it with Kohana
Update: The following instructions are deprecated, please follow the updated one here.

Even though Kohana has the ability to load vendor classes, I still find it useful to use the library approach so that loading Zend Framework libraries will be transparent. :)

Usage is exactly the same as in CodeIgniter.

01.02.
03./**
04. * Zend Framework Loader
05. *
06. * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
07. * in CI installation's 'application/libraries' folder
08. * You can put it elsewhere but remember to alter the script accordingly
09. *
10. * Usage:
11. * 1) $this->load->library('zend', 'Zend/Package/Name');
12. * or
13. * 2) $this->load->library('zend');
14. * then $this->zend->load('Zend/Package/Name');
15. *
16. * * the second usage is useful for autoloading the Zend Framework library
17. * * Zend/Package/Name does not need the '.php' at the end
18. */
19.class Zend
20.{
21. /**
22. * Constructor
23. *
24. * @param string $class class name
25. */
26. function __construct($class = NULL)
27. {
28. // include path for Zend Framework
29. // alter it accordingly if you have put the 'Zend' folder elsewhere
30. ini_set('include_path',
31. ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
32.
33. if ($class)
34. {
35. require_once (string) $class . EXT;
36. Log::add('debug', "Zend Class $class Loaded");
37. }
38. else
39. {
40. Log::add('debug', "Zend Class Initialized");
41. }
42. }
43.
44. /**
45. * Zend Class Loader
46. *
47. * @param string $class class name
48. */
49. function load($class)
50. {
51. require_once (string) $class . EXT;
52. Log::add('debug', "Zend Class $class Loaded");
53. }
54.}
55.
56.?>

No comments:

Post a Comment