I need to create custom libraries for my Codeigniter 3 application that have common functions used by all my custom library.
How to use function only in one place?
What you appear to need is a helper file, not a library.
Create a file in application/helpers directory
Eg file : mycommon_helper.php
Contents as :
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
function my_function($param1) {
printf('Hello %s', $param1);
}
Include this in the autoload file as below
$autoload['helper'] = array('url','html', .....'mycommon'); // Note _helper is omitted
After this you use the my_function($param1) anywhere in your code
$sayIt = my_function('user2473015');
You can create new custom libraries.
The library classes created by you should be placed in your application/libraries folder.
Classes should have basic prototype like this (here the name Myclass as an example):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Myclass {
public function my_function()
{
}
}
/* End of file Myclass.php */
From within any of your Controller functions you can initialize your class using the standard:
$this->load->library('myclass');
$this->myclass->my_function(); // Object instances will always be lower case
Related
I am trying to implement tcpdf library in codeigniter but getting error
Fatal error: Cannot redeclare class Pdf in C:\xampp\htdocs\project\system\libraries\Pdf.php on line 6
here code example
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */
also Download latest
TCPDF
also want to know which is best TCPDF or DOMPDF to generate quick PDF ?
When using/making custom libraries, smart move would be (as CI makers advise too) to put it in APPPATH.'libraries/' because of upgrading version could overwrite/delete added files from system directory. That is why application folder is used for - to make any kind of custom files related to CI framework not touching default files/directories.
Other than that, error you've got there says there is more than one or better say there is two classes with same name. PHP doesn't allow that and every used file and/or class must have unique name. Possible solution should be renaming your file to
BASEPATH.'Pdf_lib.php'
and class name accordingly.
But again, if you are not limited with something else, move your class to
APPPATH.'libraries/Pdf_lib.php'
with following directories and subdirectories and files of tcpdf and use it from there.
I fix this error using bellow method. its happen coz same class is call from other TCPDF included file here's the solution
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once '/tcpdf/tcpdf.php';
if (!class_exists('Pdf')) {
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
}
I would like to use http://demo.hazzardweb.net/imgPicker/docs/. I have downloaded all the files.
How can I convert it into a Codeigniter third party library or how can I call this class file in a Codeigniter controller? Is it possible to call the core php files directly in a controller to achieve this?
For example:
require dirname(__FILE__) . '/ImgPicker.php';
You can create a new library for your project.
Your library classes should be placed within your application/libraries directory, as this is where CodeIgniter will look for them when they are initialized.
Be sure that:
File names must be capitalized. For example: ImgPicker.php
Class declarations must be capitalized. For example: class ImgPicker
Class names and file names must match.
Your ImgPicker class file
Classes in general should have this basic prototype:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class ImgPicker {
// all stuff that your php library does
public function some_method()
{
. . .
}
}
Then from your controller (or model) you can load your library with $this->load->library('someclass'); and you can pass vars to the constructor dynamically like so:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('ImgPicker', $params);
You can find a lot of useful information how to create your own libraries (or convert existing php classes to CI libraries) at CI documentation.
My library Class in common/auth/auth_manager.php folder :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth_manager {
public function __construct() {
$this->allow_dev_login = TRUE;
$this->_ci =& get_instance();
$this->_ci->load->spark('flexi-auth/1.5.0/');
}}
My controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Contacts extends MY_Controller {
public function __construct() {
parent::__construct();
echo "string";
$this->load->library('auth/auth_manager');
}
In the above code the string before loading the library is working . But after loading the page is just blank . Have to use the functions from those libraries . If i use the below code
$this->auth_manager->register();
Getting the error property not defined .
your library file at wrong place
According to Codeigniter standard Keep your library in application/libraries/ directory then you can load library
$this->load->library('auth_manager');
For more follow here
or official docs :- https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
$this->load->library(); method is finding library in /application/libraries or /system/libraries folders, if you want to load library from another location then register third party folder path
$this->load->add_package_path(APPPATH.'common/auth/');
and then user load library method
$this->load->library('auth_manager');
I am trying to use PHPWord and having a difficulties to use it.
This my code within application/libraries/words.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PhpWord/Autoloader.php";
\PhpOffice\PhpWord\Autoloader::register(); \\NOT SURE IF THIS IS CORRECT
class Word extends PhpWord {
public function __construct() {
parent::__construct();
}
}
Based on PHPWord github, i must use it like this:
Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.
require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); // WHERE SHOULD I PUT THIS?
When i try to load the library from my controller as such:
$this->load->library('word');
it gives me error that says:
Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7
i tried to extends the Autoloader class, but PHP still complains that it can't found Autoloader.php.
when i do
file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php
it returns 1.
Does anyone know how to fix this?
From Russia with love;)
Use like this in library for CodeIgniter:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/libraries/PHPWord/src/PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();
class Word extends Autoloader {
}
?>
In controller like code from samples:
$this->load->library('word');
/* remove ;)
#require_once 'PHPWord/src/PhpWord/Autoloader.php';
#PhpOffice\PhpWord\Autoloader::register();
*/
// Template processor instance creation
echo date('H:i:s') , ' Creating new TemplateProcessor instance...' ;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('text.docx');
$templateProcessor->setValue('sss','123');
echo date('H:i:s'), ' Saving the result document...';
$templateProcessor->saveAs('test1.docx');
Tested it works!
I am unable to load my custom class which doesnot extend from any core class.
I have placed my custom class in a subfolder inside application/libraries.
So here is my folder structure
application
|_ libraries
|_ cgh
|_ cgh_asset.php
|_ cgh_article.php
|_ cgh_asettype.php
|_ controllers
|_ welcome.php
Class Cgh_article is a subclass of Cgh_asset
Cgh_asset.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Cgh_asset
{
public $id;
public $type;
public $title;
public $content;
public $user;
abstract public function generate_url();
function __construct()
{
$this->generate_url();
}
}
?>
Cgh_article.php :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_article extends Cgh_asset
{
function __construct()
{
parent::__construct();
$this->type=Cgh_assettype::article;
}
function generate_url()
{
$this->url="Article_URL";
}
}
?>
Cgh_assettype.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_assettype
{
const type1="type1";
const type2="type2";
const article="article";
}
?>
Controller welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article');
$this->load->view('welcome_message');
}
}
The error I get is :
Unable to load the requested class: Cgh_assettype
I must have tried all possible upper and lower case combinations of classnames, filenames, but the error is always the same.
After going through some answers, I think probably I should add one basic question here - Is it at all possible to have my own custom object types within codeigniter ... types that should be quite obvious from my question ?
This seems to work for me, so here is what I will be doing ... at least till something breaks :
In the constructor of my controller, I use require_once for my classes ... and good thing is I can combine all my classes into a single file -- my classes initially were in a single file anyways --This is my controller after the changes, and this works :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public $cgh_assettype;
public $cgh_asset;
public $cgh_article;
function __construct()
{
parent::__construct();
//$this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));
echo "Including CGH<br />";
echo "<p>Apppath is ". APPPATH. "</p>";
require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');
require_once(APPPATH.'libraries/cgh/Cgh_asset.php');
require_once(APPPATH.'libraries/cgh/Cgh_article.php');
}
public function index()
{
$iCgh_article=new Cgh_article();
echo "<p>$iCgh_article->url</p>";
$this->load->view('welcome_message');
}
}
You need to call $this->load->library for each library.
$this->load->library('cgh/Cgh_assettype');
$this->load->library('cgh/Cgh_asset');
$this->load->library('cgh/Cgh_article');
$this->load->library takes 3 parameters.
File to load
(optional) $config array
(optional) String to rename library to ($this->Renamed_library)
If you want to load multiple libraries on one line, use an array as the 1st parameter.
$this->load->library(array('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article'));
Are you library file names capitalized? (Your submitted folder structure says they are not).
I don't know about having libraries within sub-directories, but I know the file names need to be capitalized.
http://codeigniter.com/user_guide/general/creating_libraries.html
Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.
You should load library or whatever only once. If you're loading for second time, you get that error.