How to integrate codeigniter and PHPExcel? - codeigniter

always display an error message on my browser:
An Error Was Encountered
Non-existent class: IOFactory
all classes PHPExcel, i extract on library.
Here it is my controller code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Report extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
}
public function index()
{
$this->load->library('phpexcel');
$this->load->library('PHPExcel/IOFactory.php');
$objPHPexcel = PHPExcel_IOFactory::load('tandaterima.xlsx');
$objWorksheet = $objPHPexcel->getActiveSheet();
//Daftar barang (4item)
$objWorksheet->getCell('B16')->setValue('UTP');
$objWorksheet->getCell('B17')->setValue('Cross');
$objWorksheet->getCell('B18')->setValue('');
$objWorksheet->getCell('B19')->setValue('');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
$objWriter->save('write5.xls');
}
}
please help me.

Follow the instruction here
https://github.com/EllisLab/CodeIgniter/wiki/PHPExcel
Please remember to remove the PHPExcel_ part in the class name in IOFactory.php. And change the construct function from private to public

ensure that u save PHPExcel/IOFactory.php inside libraries folder and load it as $this->load->library('PHPExcel/iofactory');

You can replace this line
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
by this line
IOFactory::createWriter($objPHPexcel, 'Excel5');

First you need to place your PHPExcel folder inside thirdparty folder. Then create class file in the library folder. There you need to include thirdparty/PHPExcel file folder and extend the class. After that you can use it in your controller.

And in some Linux Server, you have to care about case.
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');

Codeiginiter 3 supports Composer to use PHPExcel:
In application/config/config.php, set $config['composer_autoload'] to TRUE.
Then you can use Composer to install PHPExcel in Codeiginiter :
composer require phpoffice/phpexcel
Further, You could try PHPExcel Helper to easier handle PHPExcel:
composer require yidas/phpexcel-helper
https://github.com/yidas/phpexcel-helper

Related

PHPCas composer package and Codeigniter 3

I am trying to make a Codeigniter 3 application to authenticate to a CAS server.
I installed the phpcas package with composer but I cant find any details or examples on how to do this. I want the access to this application to be only from authentication users.
I believe that I have to edit a config file but I cant find any to write down the cas server url, etc. Also, in the controller of the page, I have to call somehow the library but I need some example for that.
So the main question is how can i get to use phpcas inside my controller methods amd how to configure it?
I created a controller and have inside:
?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class AuthController extends CI_Controller {
public function actionLogin()
{
$this->module->casService->forceAuthentication();
$username = $this->module->casService->getUsername();
$this->module->casService->getAttribute('mail')
$this->module->casService->getAttribute('title')=='TEACHER';
//$auth->assign($casRole, $user->id);
}
public function actionLogout()
{
$this->module->casService->logout(Url::home(true));
// In case the logout fails (not authenticated)
return $this->redirect(Url::home(true));
}
}
I place to the controller file the line:
require_once(APPPATH . '/../vendor/autoload.php');
and I init with:
$phpCAS = new phpCAS;
phpCAS::client(CAS_VERSION_2_0, 'sso-test.sch.gr', 443, '', false);

codeigniter admin route is not working. it returns "The requested URL was not found on this server. "

i need to access the admin controller in codeigniter using the url mywebsite.com/admin
but it returns "The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again" error
my code is
routes.php
$route['admin'] = 'admin/adminmain';
adminmain.php (admincontroller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Adminmain extends CI_Controller {
public function index()
{
$this->load->view('admin/adminhome');
}
}
In Codeigniter 3 or above controllers name should be capitalized.
The controller file name should be Adminmain.php
controller file name should be Adminmain.php and try to define route like: $route['admin'] = 'admin/Adminmain'; and also check r u defining admin folder under controller folder

CodeIgniter: How to load my own class of static functions into a controller?

I have a class of static functions (common utility functions) that I wish to load into codeigniter. Currently I am loading it normally using an include_once(...) and it works as expected.
However, I want to load it using codeigniter's methodology. I understand that I should save my class file into the third_party directory; and that I should create a library class (saving it in the library directory) which requires my class.
Below are the three components, but it does not work.
1
// my class, saved at: APPPATH.'third_party/My_Class.php'
class My_Class
{
public static function my_static_utility_method ( )
{
return "booger" ;
}
// ...
}
2 - I understand that I am supposed to create a wrapper that obeys the CI rules of 'library' instantiation:
// saved at: APPPATH.'libraries/Library_Wrapper.php'
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Library_Wrapper
{
public function __construct()
{
require_once APPPATH.'third_party/My_Class.php';
}
}
3 - Now I want to access the static methods of My_Class from my controller:
// saved at: APPPATH.'controllers/my_controller.php'
class My_Controller extends CI_Controller
{
public function __constructor( )
{
parent::__construct();
$this->load->library( 'Library_Wrapper' ) ;
}
public function some_function()
{
echo $this->My_Class->my_static_utility_method( ) ;
}
}
Try the following steps:
Create a new controller file: application/core/MY_Controller.php. In that file you may add your static functions.
<?php
class MY_Controller extends CI_Controller {
protected function static_fn1() {
//code
}
}
The controllers which need to access the static functions may extend this class like:
File: application/controllers/Welcome.php :
<?php
class Welcome extends MY_Controller {
public function fnname() {
//code
}
}
You've almost got the 3rd step in place but not quite. Loading the library will take the same name that you loaded it with:
//Load library
$this->load->library( 'Some_name' );
//Use Library
$this->some_name->someFunction();
So in your case, you'd need to switch method which accesses the library from:
//Will throw an PHP undefined My_Class error
echo $this->My_Class->my_static_utility_method();
to Library_wrapper instead:
//from $this->load->library( 'Library_wrapper' );
echo $this->library_wrapper->my_static_utility_method();
But this presents the next problem as My_Class is a property of library_wrapper so calling it gets a bit lengthy:
echo $this->library_wrapper->My_Class->my_static_utility_method();
Which should successfully call the My_Class descendent methods if publicly accessible.
This isn't clean as you would perfer. It would better to extend My_Class into Library_wrapper instead to share the static instances:
/**
* Static helper methods:
**/
class Library_wrapper extends My_Class {
}
It is possible to bind the 'load' the library to a different name (See "Assigning a Library to a different object name" header).
You can try the solution below:
in this hierarchy $this->library_wrapper->My_Class->my_static_utility_method();
use the php function listed at Php Functions to find out the methods/variables at each point in the hierarchy. This will pin point to the exact location where the things are going wrong.
I do NOT think you need to construct a class. The easiest way would be a helper ("Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category" - just read the article from the official tutorial).
Your helper file with a name like myfunctions_helper.php will be located into /application/helpers folder and could have a structure like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if (!function_exists('my_static_fuction')) {
function my_static_fuction ( )
{
return "booger" ;
}
}
// etc...
Then you can autoload the helper declaring it to the /application/config/autoload.php.
$autoload['helper'] = array('url','myfunctions');
If you need to construct a class then a library like #MackieeE wrote will do the job (check the tutorial for more info).

Codeigniter: Unable to locate the specified class: Exceptions.php

Here's my controller:
$html = $this->load->view('print_po', $po, TRUE);
$this->load->library('pdf');
$pdf = $this->pdf->load();
Now I've tried and comment each line and the one that shows the error is:
$pdf = $this->pdf->load();
Here's my library class in application/libraries:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class pdf {
function pdf()
{
$CI = & get_instance();
log_message('Debug', 'mPDF class is loaded.');
}
function load($param=NULL)
{
include_once APPPATH.'/third_party/mpdf/mpdf.php';
if ($params == NULL)
{
$param = '"en-GB-x","A4","","",10,10,10,10,6,3';
}
return new mPDF($param);
}
}
The error comes only after moving the code from one server to another(the error happens on a CentOS server which I bet is case sensitive). My question here would be: what should I modify so codeIgniter loads Exceptions.php normally?
First, remove the forward-slash at the first of /third_party phrase:
include_once APPPATH.'third_party/mpdf/mpdf.php';
CodeIgniter defines APPPATH constant with a trailing slash. Take a look at index.php:
define('APPPATH', $application_folder.'/');
Second, make sure that the file/folder names are as the same as you have wrote. It's better to keep them all lowercase. Here is a related topic.
Update:
The class name should be Capitalized. In this case change the pdf to Pdf:
class Pdf {
// ...
}
From CI Documentation:
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.

Codeigniter 2.0 Can not extend core library

not sure what is the problem? My class that is in Application/core folder, and here it is:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Image_lib extends CI_Image_lib {
public function __construct() {
parent::__construct();
}
function tesit($msg) {
log_message('error', $msg);
}
}
I get this error :
Fatal error: Call to undefined method CI_Image_lib::testit()
when I call it like this : $this->image_lib->testit('not working');
What I am missing, this is so strange.
Only the following classes are core classes all others should be extended in library folder.
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Benchmark
Config
Controller
Exceptions
Hooks
Input
Language
Loader
Log
Output
Router
URI
Utf8
Move your class to the application/library folder and it will override the one in the system folder when you call it.

Resources