Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 48 revisions

[b]This is a tutorial how to integrate PEAR into Codeigniter by [email=[email protected]]Fabian Wesner[/email][/b]

[h4]What is PEAR?[/h4] [quote]PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

  • A structured library of open-source code for PHP users

  • A system for code distribution and package maintenance

  • A standard style for code written in PHP, specified here

  • The PHP Extension Community Library (PECL), see more below

  • A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project governed by its developers. PEAR's governing bodies are subdivided into the PEAR Group, Collectives, and a President. PEAR's constitution (adopted in March 2007) defining these groups is documented here. The PEAR project was founded in 1999 by Stig S. Bakken and quite a lot of people have joined the project. [/quote] [url]http://pear.php.net[/url]

[h4]PEAR and Codeigniter[/h4] [b]1. Preparation[/b] To use some of PEARs Libraries in Codeigniter you need to create a new folder. [code]system/application/pear[/code] You must copy the PEAR.php to this directory. [code]system/application/pear/PEAR.php[/code]

Then you copy some more PEAR-Directories and Classes. Pay attention to dependencies! Example: [code]system/application/pear/HTTP/Request.php system/application/pear/Net/Socket.php system/application/pear/Net/URL.php[/code]

[b]2. Enable Hooks[/b] [b]Note: the purpose of the hook is to set the include path to include the path to PEAR. As an alternative to a hook, you could put the same ini_set() function in your config.php file or the main CI index.php file.[/b] You need to enable hooks inside the config.php: [code]$config['enable_hooks'] = TRUE;[/code]

Open hooks.php and add a hook: [code]$hook['pre_controller'][] = array( 'class' => 'Pear_hook', 'function' => 'index', 'filename' => 'pear_hook.php', 'filepath' => 'hooks' );[/code]

Then create a new file called pear_hook.php: [code]system/application/hooks/pear_hook.php[/code] [code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pear_hook{ function index(){ // OS independent ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/pear/'); // on Apache // ini_set('include_path',ini_get('include_path').':'.BASEPATH.'application/pear/'); // on Windows // ini_set('include_path',ini_get('include_path').';'.BASEPATH.'application/pear/'); } }

?>[/code]

[b]3. Use It![/b] create a library called PearLoader. [code]system/application/libraries/Pearloader.php[/code]

Pearloader.php: [code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pearloader{ function load($package, $class,$options = null){ require_once($package.'/'.$class.'.php'); $classname = $package."_".$class; if(is_null($options)){ return new $classname(); }else{ return new $classname($options); } } }

?> [/code]

Thats it!

You can use the Pearloader in a codeigniter style. Load the Library and call $this->pearloader->load('[i]Packagename[/i]','[i]Classname[/i]');

The example performes a HTTP_REQUEST to yahoo: [code]function example(){ $url = 'http://www.yahoo.com'; $this->load->library('pearloader'); $http_request = $this->pearloader->load('HTTP','Request'); $http_request->setURL($url); $http_request->sendRequest(); echo $http_request->getResponseBody(); } [/code]

Thanks to [url=http://www.4webby.com/freakauth/tutorials/using-zend-framework-components-in-code-igniter]4webby.com[/url]

Clone this wiki locally