CodeIgniter PHPWord using as third_party - php

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!

Related

Unable to load the requested class: PHPExcel

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

tcpdf codeigniter getting error

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();
}
}
}

Common functions for codeigniter custom libraries

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

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

wiredesignz modular extensions extending controller

I am using WireDesignz modular extensions with great success so far. I now have the need to extend a controller within the module. I have created the new controller and the original, now extended, controller and they work fine outside of the HMVC but when I put them inside the module folder and call the new controller it days is can't find the controller it's extending... even though it's right there in the same directory. If I call the original one it's all fine. I'm not sure where to go with this as I can't find anyone with the same problem online. Any ideas? Here's a bit more:
Original controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends MY_Controller {...
New controller, in same directory:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar_new extends Calendar {...
Results in:
Fatal error: Class 'Calendar' not found in /home/d/e/demo/web/public_html/application/modules/calendar/controllers/calendar_new.php on line 2
Thanks.
The base controller class which you are extending is not being included as a resource. Codeigniter will not automatically try to load base classes.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('Calendar.php');
class Calendar_new extends Calendar {...

Categories