📜 ⬆️ ⬇️

Codeigniter with PEAR


In this article I will explain how to connect the PEAR libraries in the Codeigniter framework. If PEAR is installed on the system by default and you have access to install individual libraries, then everything is simple. Otherwise, you have to connect all the pens. Immediately give a link to the source: codeigniter.com/wiki/PEAR_integration . Below, I translated this manual, added some corrections to my code, in my opinion.


1. Preparation
To use PEAR libraries you need to create a new directory.
system/application/pear
Next, copy PEAR.php to this directory.
system/application/pear/PEAR.php
Then copy the necessary libraries. Pay attention to library dependencies.
Example:
system/application/pear/HTTP/Request.php
system/application/pear/Net/Socket.php
system/application/pear/Net/URL.php


2. Connect Hooks

Note: You must add the path to the PEAR libraries in the include_path. As an alternative to the methods described below, you can insert the ini_set () function into your config.php or into the main CI index.php file.
')
You need to enable the use of hooks in config.php.
$config['enable_hooks'] = TRUE;
Next, open hooks.php and add the following hook:
$hook['pre_controller'][] = array(
'class' => 'Pear_hook',
'function' => 'index',
'filename' => 'pear_hook.php',
'filepath' => 'hooks'
);


Then create a new file pear_hook.php.
system/application/hooks/pear_hook.php
We write there.
<?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/');
}
}

?>


3. Create a PearLoader class.

system/application/libraries/Pearloader.php:

<?php

class Pearloader {
function load($package, $class = NULL, $options = NULL) {
if (is_null($class)) {
require_once $package . '.php';
$classname = $package;
} else {
require_once $package . '/' . $class . '.php';
$classname = $package . '_' . $class;
}
if (($count = func_num_args()) > 2) {
$params = '';
for ($i = 2; $i < $count; $i++) {
eval("\$var$i = func_get_arg($i);");
$params .= "\$var$i" . (($i + 1 == $count) ? '' : ', ');
}
eval("\$instance = new $classname($params);");
return $instance;
} else {
return new $classname();
}
}
}

?>


It's all!

You can use the Pearloader in Codeigniter style. First we load the library and call the load method:
$this->pearloader->load('Packagename','Classname');

For example, the following is a request to yahoo.

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();
}

Source: https://habr.com/ru/post/37929/


All Articles