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();
}
}
Related
I want to set my session variable in every function of the class. If the session is not set in any of the function , then it should be redirected to home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
public function masteradmin()
{
if($this->session->userdata('name')){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
else
{
redirect('/', 'refresh');
}
}
public function reseller()
{
}
}
Use constructor for that. In OOP constructor is the first method of any class which is called. So, what you can do is, create a constructor and check for the session variable. If the variable exists, do nothing, it will automatically call the function. If the session variable does not exist, redirect the user to the home page.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
function __construct(){
parent::__construct();
if(!$this->session->userdata('name')){
redirect('/home');
}
}
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
public function reseller(){
}
}
?>
create super class in core folder like MY_Controller.php
and there you can define session and then call that super class in every controller extends. for example:
class App_config extends MY_Controller {
}
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 !
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.
I am having issues with my Code Ignitor, in that I load my model the code fails, without leaving any trace.
I am attempting to load my model like so
<!--CONTROLLER-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concierge extends Public_Controller {
public function submit()
{
$this->load->model('Concierge_model');
}
My model is set up as below:
<!--MODEL-->
<?php
class Concierge_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function saveRequest($action, $owner)
{
$query = $this->db->query('select * from table');
return $query;
}
Does anyone see what might be the issue that is causing my problem? I am using CodeIgnitor 2.1.2.
Assuming Public_Controller is something custom, make sure that class extends CI_Controller and also make sure the parent's contruct function is called there.
class Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
I don't know what the problem was, I deleted the model, view, and controller and recreated them. This fixed the issue, but sadly didn't shed any light onto what caused it.
So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...