How do you load a helper within a library in codeigniter? - 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);

Related

How to load a library from composer?

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.

CodeIgniter accessing models from helper

I'm writing a CodeIgniter helper for e-mail functions, and need currently in each function I'm writing:
$CI = &get_instance();
$CI
->email
//etc
It's not a big problem, but I was wondering if there was a way of loading the CI instance once for all functions? Something like a constructor method?
If you are absolutely set on defining your functions inside of a helper rather than extending CodeIgniter's email library then you can do this:
email_helper.php
<?php
// Assuming $CI has not been set before this point
$CI = &get_instance();
function some_email_function()
{
$GLOBALS['CI']->email;
}
unset($CI);
You can actually do this.
Here's an example
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example {
public function __construct()
{
$this->CI =& get_instance();
}
public function functionExample(){
$this->CI->load->module('fullpage');
if($this->CI->fullpage->my_function() ){
echo 'It works!';
}
}
}

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 to load module in my own created library codeigniter

class Mylib
{
function show_lib()
{
$obj=& get_instance();
$obj->load->module(‘login_check’);
$var=$obj->login_check->get_all_table_data();
print_r($var);
}
}
ERROR:-
Fatal error: Call to undefined method CI_Loader::module()
I Hope This Will Work --> check this code
class Mylib
{
function show_lib()
{
protected $ci;
$this->ci = &get_instance();
$this->ci->load->library(‘login_check’);
$var=ci->load->login_check->get_all_table_data();
return $var;
}
}
Not module, it's a library
use:
$this->load->library();
Working with modules in CI is called HMVC - Hierarchical Model View Controller.
There is a beautiful modular extension for CI to work with - Modular Extensions - HMVC
By using this extension you can create and work with modules in CI.
After setting up HMVC Extension you can call modules from your controller.
$controller = $this->load->module('module_name/controller_name');
echo $controller->method();
Few tutorials to get started:
http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/
http://www.extradrm.com/blog/?p=744
If you are using codeigniter 3 you need to use Codeigniter Object to load modules,helpers..etc. Assign Codeigniter object to a variable and use it. $CI =& get_instance() Sample code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CustomLibrary{
public $random_number;
public function __construct(){
$CI =& get_instance();
$CI->load->helper('string');
}
}
For more information about this visit http://www.webnsyntax.com/

Create Codeigniter Helper -> undefined method

I created a helper "session_helper.php" in application/helpers/ folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('is_login'))
{
function is_login()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != TRUE) {
redirect('login');
}
}
}
And "Configuracion" Controller:
class Configuracion extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$this->load->helper('session');
$this->is_login();
}
}
The problem is when I call the controller "http://localhost/proyect/configuracion" I get the following error:
Fatal error: Call to undefined method Configuracion::is_login() in C:...\application\controllers\configuracion.php on line 15
I read the manual and apparently everything is correct ... what is wrong?
"is_login" is a function, not a method. Just replace $this->is_login(); with is_login();.
Helpers are not methods, they are just simple function calls.
Take a look at helpers in the User Guide: http://codeigniter.com/user_guide/general/helpers.html
Loading a helper:
$this->load->helper('url');
using its helper functions:
<?php echo anchor('blog/comments', 'Click Here');?>
where anchor() is the function that is part of the loaded helper.
Also I would urge you to stay way from calling a helper 'session' make it something more descriptive, as it might become confusing later on. Just a suggestion, its entirely up to you.

Resources