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.
Related
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);
I have downloaded the ci-hmvc-master and setup it.
Now in that I have created a one hook at pre_controller hook point.
In that hook, I have created one function that print the current class and method.
my code looks like this:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Authentication{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function is_loggedin(){
$class = $this->CI->router->fetch_class();
echo $class;
}
}
This code is working fine. I got the class name.
Now my issue is that when I use this same code in HMVC which is created from normal MVC (replacing some of the folders and files) then I got the error like:
Call to a member function fetch_class() on null
Means The pre_controller hook executes before the super object has been fully constructed, so get_instance() can't work.
So, why it is working in HMVC extension which I have downloaded and why it is
not working in simple HMVC which is created from MVC.
Is there any configuration is missing?
I have in my core folder, 2 controllers:
MY_Controller
MY_AdminController
Both extend CI_Controller. First one works great, but second one no, when I call the controller which is inheriting from MY_AdminController I get an error:
Fatal error: Class 'MY_AdminController' not found
After doing some research I found:
https://codeigniter.com/user_guide/general/core_classes.html
In this document it says you use "MY_" prefix (possible to change it in config) to extend core classes, I am doing that.
What am I missing?
UPDATE
I am wondering if the problem is because since I am creating a file inside "core" folder, CI checks if it does exist an original on its own core with same name but prefix CI?
to allow any new class . means which class are note defined in system/core
solution could be as follow.
Try putting below function at the bottom of config file
/application/config/config.php
function __autoload($class) {
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/'. $class . '.php' );
} }
My core class My_head not found in codeigniter
Here is a good explanation as to why you can't do it the way you have described. Essentially CodeIgniter looks for ['subclass_prefix'] . $Classname, e.g. 'MY_' . 'Controller'. And here is the same question for CI2
Solution:
Put both MY_Controller and MY_AdminController in MY_Controller.php
MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
class MY_AdminController extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
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).
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