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

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.

Related

How to get $config['imgURL_Path'] value from CodeIgniter's file config.php NOT from CI controller?

I want to get some constants, defined in CodeIgnitor in /application/config/config.php, but when i am outside CI controller. For example, i have a file that generates image thumbs or something else, that is outside CodeIgniter mvc framework. Cause the file /application/config/config.php has a string:
defined('BASEPATH') OR exit('No direct script access allowed');
i can't just do something like :
include '/application/config/config.php';
$imgPath = $config['imgURL_Path'];
I tried to add this strings, but no result:
if (defined('STDIN'))
{
chdir(dirname(__FILE__));
}
if (($_temp = realpath($system_path)) !== FALSE)
{
$system_path = $_temp.'/';
}
else
{
// Ensure there's a trailing slash
$system_path = rtrim($system_path, '/').'/';
}
// Is the system path correct?
if ( ! is_dir($system_path))
{
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
exit(3); // EXIT_CONFIG
}
define('BASEPATH', str_replace('\\', '/', $system_path));
include '/application/config/config.php';
$imgPath = $config['imgURL_Path'];
i also tried to make a file ci.php with this content:
<?php
// file: ci.php
ob_start();
require_once '../index.php';
ob_get_clean();
return $CI;
?>
and use it like:
$CI = require_once 'ci.php';
$imgPath = $CI->config->item('imgURL_Path');
but also no luck. What can be the solution?
If you want an URL_path or a variable that can be accessed anywhere, you can use helper.
1 Create a file in folder helpers, let's say you named it myvariable_helper.php //make sure you add '_helper' before the .php.
2 Open autoload in config folder, then search for $autoload['helper']
, insert your newly created helper there like this: $autoload['helper'] = array(url, myvariable)
3 Save the autoload, then open myvariable_helper.php, and add a function. Let's say you add an URL to a function like this:
<?php
function img_path()
{
$imgURL_Path = 'www.your_domain.com/image/upload';
return $imgURL_Path;
}
4 Call the function anywhere (even outside your MVC folder), using the function name, for example:
public function index()
{
$data['url'] = img_path(); //assign the variable using return value (which is 'www.your_domain.com/image/upload')
$this->load->view('Your_View', $data); //just for example
}
Note:
This helper can't be accessed from browser (like your model folder), so it's safe

How to extend Code igniter cache class Redis class

I am using redis for caching in one of my assignment. I am using CI default redis library for this purpose.
Now the issue with library is that it has some specific set of method which are used to set, get, delete , increment and decrement the redis keys & values.
I want to additional function of redis like lpush, rpush,lrem, lrange etc.
So to achieve this , i am trying to extend default CI redis class. which i am putting in application/libraries/driver/cache_redis_extended.php.
my code for this class is as follow.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Cache_redis_extended extends CI_Cache_redis
{
function __construct()
{
parent::self;
}
public function rpush($list, $data)
{
$push = $this->_redis->multi(Redis::PIPELINE);
return $push->rpush($list, json_encode($data));
}
public function lrem($list, $data)
{
if((is_string($data) && (is_object(json_decode($data)) || is_array(json_decode($data))))) {
$data = $data;
}else{
json_encode($data);
}
return $this->_redis->lrem($list,-1, $data);
}
public function __destruct()
{
if ($this->_redis)
{
$this->_redis->close();
}
}
}
and in my model I am loading this class as follows
$CI->load->driver('cache', array('adapter' => 'redis'));
But I get this error:
Unable to load the requested class: cache_redis_extended
Any help is appreciated for this issue.
As I can see your driver name is not started with Capitalized , so
it can be possible the cause of your issue.
Because according to codeigniter the naming rule of a class as follows
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.
change your file name
from cache_redis_extended.php
to Cache_redis_extended.php
I hope it will be helpful for you.

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).

How to integrate codeigniter and PHPExcel?

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

CodeIgniter controller gives 404 or it can't load the view file

I have the following issue with a simple controller in a CodeIgniter install.
In controllers/pages.php the Pages.php controller looks at the URL segments and load static files from folder and sub-folders inside the /views/pages directory.
Example:
If I have site.com/buy, then it would load buy.php form /views/pages
If the URL is site.com/buy/go, then it would load go.php from
/views/pages/buy, while site.com/buy would now be index.php from
/views/pages/buy
It was changed to add another sub-folder (e.g. site.com/buy/go/why from /views/pages/buy/go/why);
The controller:
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->view();
}
private function view() {
$url_string = $this->uri->uri_string();
if (!file_exists(APPPATH . 'views/pages/'. $url_string. '/index.php')) {
if (!file_exists(APPPATH . 'views/pages/' . $url_string . '.php')) {
show_404();
} else {
$path = $url_string . '.php';
}
} else {
$path = $url_string . '/index.php';
}
$this->load->view('pages/' . $path);
}
The issue is that I get a 404 regardless of URL.
If I remove pages/ from $this->load->view it would throw another error: unable to load the file; but it does get the file right. (e.g. unable to load buy/go.php, while the URL issite.com/buy/go`).
I've just been playing around trying a few different things out to see what was going on.
I won't go into massive detail about everything I tried, but essentially there're only a couple things to change. Using the __construct() doesn't appear to work in the way you're trying to use it (ie simply passing all requests to one method). Instead, you'd need to either manually specify all the methods or use routes - so that's what I've done.
Adding the following line to your config/routes.php will redirect all requests to the view method:
$route['pages/(:any)'] = 'pages/view';
Once you've got the route in place there's no need for the __construct. The only thing left is to remove the pages/ part of the path, which you may have already done. I've tested the following code as far down as /pages/buy/go/test (3 levels), but in theory it should work the same at any nesting level.
class Pages extends CI_Controller {
public function view()
{
$url_string = $this->uri->uri_string();
if (!file_exists(APPPATH . 'views/'. $url_string. '/index.php'))
{
if (!file_exists(APPPATH . 'views/' . $url_string . '.php'))
{
show_404();
}
else
{
$path = $url_string . '.php';
}
}
else
{
$path = $url_string . '/index.php';
}
$this->load->view($path);
}
}
As a sidenote, you don't need to add the file extension when loading views unless it's something other than .php, though it doesn't matter either way.

Resources