-
Notifications
You must be signed in to change notification settings - Fork 0
PEAR integration
[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] 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 write a new hook called pear_hook.php: [code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pear_hook { /**
- includes the directory application\my_classes\ in your includes directory
*/ function index() { // on Apache // ini_set('include_path',ini_get('include_path').':'.BASEPATH.'application/pear/'); 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/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]