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');
Related
I am using Ashimante Rest Server API in my project.
This is what I did:-
I have put the Rest.php controller in the application/libraries
folder
I have created webservice-v1 folder inside the
application/controllers/ folder i.e. application/controllers/webservice-v1
I have Controller files rest_api_common.php and
webservice_test.php inside the webservice-v1 folder.
This is my rest_api_common.php code:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH . 'libraries/Rest.php');
class Rest_api_common extends REST {
// some variable declarations //
public function __construct() {
parent::__construct();
// loading models and custom config files//
}
}
I want the variables and functions available to all the webservice functions. So my webservice_test.php file code goes like this:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('rest_api_common.php');
class Webservice_test extends Rest_api_common
{
function __construct(){
parent::__construct();
}
public function mongodata_post()
{
$this->response(array(),200);
}
}
I have defined route like this:-
$route['rest-api/v1/mongodata'] = "webservice-v1/webservice_test/mongodata_post";
Using postman app, I am calling the API like this using POST method:-
http://testdomain.com/rest-api/v1/mongodata
I am getting this response:-
{"status":false,"error":false}
What am I doing wrong?
First, I download the PHPExcel in this URL : https://github.com/PHPOffice/PHPExcel
And I unzip this file and take PHPExcel.php and PHPExcel Folder.
I put them in libraries folder in Codeigniter.
I load the PHPExcel but it returns this message.
<?php
class ExportSample extends REST_Controller{
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('PHPExcel');
}
}
?>
Error : Unable to load the requested class: PHPExcel
I think put it in libraries and just load the library but maybe it is not.
Is any mistake when I setting it?
Please give me any idea
update
Error message : require_once(): Failed opening required '/var/www/html/appservice/application//third_party/PHPExcel.php' (include_path='.:/usr/share/php:/usr/share/pear') in
<b>/var/www/html/appservice/application/libraries/Excel.php
I think your path is wrong.
Your library folder should be in:application/third_party/PHPExcel/PHPExcel.php
After that you need to create one library excel.php in application/libraries/ folder.
Put this code in excel.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPExcel/Classes/PHPExcel.php";
require_once APPPATH."/third_party/PHPExcel/Classes/PHPExcel/IOFactory.php";
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
And use this code in your controller ExportSample.php:
<?php
class ExportSample extends REST_Controller{
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('Excel');
}
}
?>
I hope it will work for you!
Try :
require_once APPPATH . "/third_party/PHPExcel.php";
And clone lib phpExel in application/third_party/
Dont put them in your libraries folder, just put it in your application/third_party folder
it should look like
...application/third_party/PHPExcel.php
...application/third_party/PHPExcel/....
After that create a library called Excel.php or something like that and put it in your library folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel
{
public function __construct()
{
parent::__construct();
}
}
your library folder shoud look like ../application/libraries/Excel.php
And in your Controller
class ExportSample extends REST_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('Excel');
}
}
that should pretty much get the job done
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
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.