How to load a library from composer? - codeigniter

I load a composer library suited for CodeIgniter called SteeveDroz\Asset, that I can access without problem with $asset = new SteeveDroz\Asset.
I would like to be able to load it with CodeIgniter $this->load->library('SteeveDroz\Asset'), but I get the error message
Unable to load requested class: SteeveDroz\Asset
Is it possible to achieve what I want? If yes, how?

As mentionned Alex in his comment, it is required to make an adapter library. I created an all purpose class for that:
application/libraries/ComposerAdapter.php
class ComposerAdapter
{
private $object;
public function __construct($object)
{
$this->object = $object;
}
public function __call($method, $args)
{
return call_user_func_array([$this->object, $method], $args);
}
}
application/libraries/Asset.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require('ComposerAdapter.php');
class Asset extends ComposerAdapter
{
public function __construct()
{
parent::__construct(new SteeveDroz\Asset());
}
}
application/config/autoload.php
// ...
$autoload['libraries'] = array('asset');
// ...

if you are using CodeIgniter 3 you can modify application/config/config.php and set
$config['composer_autoload'] = TRUE
or
$config['composer_autoload'] = FCPATH .'vendor/autoload.php';
this will automatically load all your composer dependencies.

Related

CodeIgniter + PHPExcel: What is best way to call this library to CodeIgniter?

I have PHPExcel, and I want to add it to CodeIgniter.
Where is the best to copy the files from all CodeIgniter folders, and how do I call it in my controllers/models for regular usage?
EDIT:
It's a whole folder that I need to move, and call the main file: PHPExcel\IOFactory.php
You may add PHPExcel to library in Codeigniter, E.g:
application > libraries > PHPExcel.php
Let say in PHPExcel.php library file:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class PHPExcel
{
public function __construct()
{
//
}
public function some_function()
{
return 'some_function';
}
}
To call PHPExcel library, in your Controller. Let say I named it as
My_controller.php:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class My_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//Call PHPExcel class
$this->load->library('PHPExcel');
}
public function index()
{
echo $this->PHPExcel->some_function();
}
}
I followed this tutorial: https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
Got my answer !

How do I extend own library?

I am new in codeigniter and want to add a library to my first ci app which extends another library
class Mylib {}
/application/libraries/Mynewlib.php:
class Mynewlib extends Mylib{}
Where do I have to put Mylib.php and how do I load Mylib?
Thanks!
I'm still looking for best pratice in this case.
In my case, I have 2 classes should extend from a parent.
(Payment - parent class;
Payment_paypal - interact with Paypal;
Payment_nganluong - interact with Ngan Luong, my domestic gateway)
With each payment gateway, I have to write some properties, method to process but almost base properties and method of theme are the same.
My solution: I created 4 file:
Payment_base.php
class Payment_base {
// base properties and method
}
Payment.php
require_once (APPPATH.'/libraries/Payment_base.php');
// this is an instance of payment_base,
// when you want to use base method and properties
// just call $payment->...
//
class Payment extends Payment_base {
public function __construct(){
parent::__construct()
}
}
Payment_paypal.php
require_once (APPPATH.'/libraries/Payment_base.php');
class Payment_paypal extends Payment_base{}
//////////////////////////////////////////
require_one 'Payment_base.php';
class Payment_paypal extends Payment_base {
// properties and method
}
4, Payment_nganluong.php
require_once (APPPATH.'/libraries/Payment_base.php');
class Payment_nganluong extends Payment_base {
// properties and method
}
That's it, and in controller:
$this->load->library('payment');
$this->load->library('payment_paypal');
$this->load->library('payment_nganluong');
$this->payment->myMethod(); // base method
$this->payment_paypal->charge(); // call to paypal to charge money
$this->payment_nganluong->checkout(); // check out with Ngan Luong
// no need to load and use payment_base
Hope some helpful for you.
This works for me.
library file Mylib.php
class Mylib {
function test()
{
return 1;
}
}
library file Mynewlib
require_once('Mylib.php');
class Mynewlib extends Mylib{}
controller
$this->load->library('Mynewlib');
echo $this->Mynewlib->test();
you create a file in application/libraries/mylib.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MyLib
{
function __construct()
{
// initialize variables if you want
}
public function my_function($something)
{
$out = $something . " World";
return $out;
}
}
/* End of file mylib.php */
then in your controller you call your library like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class my_controller extends CI_Controller
{
public function index()
{
$this->load->library('mylib'); // call the custom class MyLib
$term = $this->mylib->my_function("Hello");
echo $term; // Hello World
}
}
/* End of file my_controller.php */
/* Location: ./application/controllers/my_controller.php */
Hope that helps
see http://www.codeigniter.com/userguide2/general/creating_libraries.html for more details
You could use get instance in library to load another library in
<?php
class MyLib {
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('mylib2');
}
public function something() {
$this->CI->mylib2->some_function();
}
}

Autoload libraries in Codeigniter

I am a newbie to Codeigniter.
I have 3 libraries in autoload in config.php .
But in one of my controllers I don't want to load the libraries. Is this possible?
If you need any library throughout the application you can load it in the config file and it will be auto loaded. But if you need a library only in a specific controller you can load it in the controller where you need it.
Class test Extends CI_Controller{
function index()
{
$this->load->library('mylibrary');
$this->mylibrary->somemethod();
}
}
Or if you need library through out the controller you can load it in the constructor.
Class test Extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->library('mylibrary');
}
function index(){
$this->mylibrary->somemethod();
}
function test(){
$this->mylibrary->someothermethod();
}
}
Extend CI_Controller in your libraries.
Something like this:
class MyLibrary extends CI_Controller {
var $ci;
function __construct() {
$this->ci = &get_instance();
$route = $this->ci->router->fetch_class();
if( $route == strtolower('YourController') ) return false;
}
}
you can remove libraries from autoload file. then they will not be activ in the framwork.
If you want to use them, you can call them in constructors if you want them in a class. If you want to use them in just method, you load them in the method.

Autoload controller in CodeIgniter

I have question to you.
I try add to my page calendar and some events in this calendar. I know how I can load calendar in page, but I didn`t now how I can load this calendar on every page automatically.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends CI_Controller {
function index()
{
$data = array(
3 => 'Polska - Anglia',
);
$this->load->library('calendar');
$vars['calendar'] = $this->calendar->generate('', '', $data);
$this->load->view('main/calendar', $vars);
}
}
and In view I call:
<?php echo $calendar;?>
I use CodeIgniter 2.1.3
Instead of creating controller for calendar, create a library class and then add it to autoload configuration file
class MyCalendar{
public function get()
{
$CI =& get_instance();
$data = array(
3 => 'Polska - Anglia',
);
$CI->load->library('calendar');
return $CI->calendar->generate('', '', $data);
}
}
Add this library to autoload file and then you can call it anywhere you want by using this statement.
$data['calendar'] = $this->MyCalendar->get();
You can autoload your library by changing the application/config/autoload.php file.
Find :
$autoload['libraries'] = array();
Replace by :
$autoload['libraries'] = array('calendar');
To load the same calendar on all your pages, I suggest to build a parent controller in the application/core folder.
abstract class BaseController extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
$calendarData = array(
3 => 'Polska - Anglia'
);
$this->data['calendar'] = $this->calendar->generate('', '', $calendarData);
}
}
You can then extend that BaseController class on all your controllers.
class Calendar extends BaseController {
function index()
{
$this->load->view('main/calendar', $this->data);
}
}
Be sure to always use $this->data to build on the protected member of your class.
Lastly, your BaseController will not be autoloaded, you probably don't want to include it everywhere. I suggest you to add the following method at the end of your autoload or config file.
/**
* Extra autoload function. Loads core classes automatically.
* #param type $class The class to load.
*/
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
This will allow you to autoload any classes you have in your application/core folder. This solution might seems complex, but once it's setup. You can add functionality to your BaseController that is applicable for all pages, for example, header and footer stuff.
Hope this helps!

How do you load a helper within a library in codeigniter?

I created a library for API access, and I created a seperate helper for common functions used by the library. In codeigniter, new libraries can access native classes by creating an instance of themselves using...
$example_instance = & get_instance();
I did this, loaded my helper- but every time the helper function is called i get the "trying to access a non-object" error. What am I doing wrong?
Here's what I have
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class api_example {
private $api;
public function __construct(){
$this->api = & get_instance();
$this->api->load->helper('helper_test');
}
public function search_recent($param){
$string = $this->api->helper_test->connect($url); //error!!!
return $xml;
}
}
/* End of file */
CodeIgniter helpers should be functions, not classes.
Try simply:
$string = connect($url);
That's not how you call a function from a helper. Helper functions aren't part of the CodeIgniter object. They're just functions.
$string = connect($url);

Resources