Unable to load the requested class: PHPExcel - php

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

Related

Session class not found when loading third party libararies

I am trying to use a third party library for openfire in my Codeigniter Application.
So I have put the libarary in third party folder, and have created an index.php file where I configure my Library.
Then I created a class file in library folder called index.php and I call the third party library like this:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Open extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
require_once APPPATH.'third_party/openfire/index.php';
}
}
Finally created a controller called user.php and tried to load this libaray using :
$this->load->library('Open');
But on screen this shows me error:
Unable to locate the specified class: Session.php
Its an unexpected error! what can be the possible reasons for this ? If i stop load this library everything works fine.
And I have already loaded Session in autload.php
class Open extends MY_Controller
Try not extending the MY_Controller

CodeIgniter - Hyperlink creation

I have tried to create a hyperlink to another page using anchors however it doesn't seem to work.
My function is called calendar and its stored in calcontrol.php within a controllers directory.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}
?>
I have tried other methods but they do not seem to work.
edit : this is my a href
Link
Filename and classname must be the same, otherwise CI will not be able to load it. And if using CI 3, make sure your class name and filename are both Ucfirst. If you have everything set up like this, then you should be able to view the page through http://yourdomain.com/calendar/calendar
Try this code. You forgot to load url helper. File Calendar.php - nogice capital C if CI3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}

Library Not loading in Controller

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');

CodeIgniter PHPWord using as third_party

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!

Call to a member function userdata() on a non-object

Hi everybody I have a code that give me this error
Fatal error: Class 'MY_Controller' not found in C:\wamp\www\project\application\controllers\admin\home.php on line 3
I have no idea why it's showing this error…
The code of C:\wamp\www\project\application\controllers\admin\home.php is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
function index()
{
redirect('admin/login');
}
function logout()
{
$this->session->unset_userdata('logged_in');
//session_destroy();
redirect('admin/login');
}
}
?>
The code of C:\wamp\www\project\application\libraries\MY_Controller.php is
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
}
And also if I place
class Home extends CI_Controller
instead of
class Home extends MY_Controller
in the
C:\wamp\www\project\application\controller\admin\home.php
file and try to load the
C:\wamp\www\project\application\libraries\MY_Controller.php
in the constructor of
C:\wamp\www\project\application\controllers\admin\home.php
it shows
Call to a member function userdata() on a non-object
Why so?
You need to put class files to core instead of library folder when you extending System classes. Put MY_Controller.php in core folder.
Refer to the documentation: http://codeigniter.com/user_guide/general/core_classes.html
Core controllers need to be stored in application/core/
So when you extend an object, it will look for it there. Library folder is used for storage of 'external' libraries, which you must explicitly include in your controller:
Ex:
$this->load->library('class name');
Info on libraries here: http://codeigniter.com/user_guide/general/libraries.html

Categories