codeigniter: extending common controller - codeigniter

I've read all the post I found regarding this issue but nothing works. I'm using Codeigniter 2.02 in a LAMP with Apache2.2 and PHP5.3.2
I'm trying to create a common controller from which my common controllers will inherit so I can do common tasks there.
I have this:
file: parent_controller.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Parent_controller extends CI_Controller {
public function Parent_controller()
{
parent::__construct();
}
public function index() {
echo "Hi!";
}
}
file: welcome.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends Parent_controller {
public function __construct()
{
parent::__construct();
}
}
I've tried the next solutions I've found, but none of them are working:
public function __contstruct() instead of public function Parent_controller()
parent::Parent_controller();
put the parent_controller.php file into core
put the parent_controller.php file into controllers
adding this to config/config.php:
function __autoload($class){
if (file_exists(APPPATH."(controllers|core)/".$class.EXT)){
require_once(APPPATH.'(controllers|core)/'.$class.EXT);
}
}
Thank you all.

Take a look at this post from Phil Sturgeon:
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
The key is using the native autoload as explained in his post:
/*
| -------------------------------------------------------------------
| Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/'. $class . EXT );
}
}
NOTE
As a note, you'll want to put all of your "base" controllers in the core folder for CI2+

This bit is correct
public function __contstruct() instead of public function
Parent_controller()
But what you're looking for is the MY_ prefix. So if you create the controller in the /application/libraries/ folder and call the file MY_Controller.php and the class MY_Controller it'll work.
You can also change the MY_ prefix to whatever you'd like in the config.php file. Look for:
/*
|--------------------------------------------------------------------------
| Class Extension Prefix
|--------------------------------------------------------------------------
|
| This item allows you to set the filename/classname prefix when extending
| native libraries. For more information please see the user guide:
|
| http://codeigniter.com/user_guide/general/core_classes.html
| http://codeigniter.com/user_guide/general/creating_libraries.html
|
*/
$config['subclass_prefix'] = 'MY_';
For further reading and a more depth explanation see http://codeigniter.com/user_guide/general/core_classes.html

Also note it doesn't load a multitude of files. It simply looks for 1 controller called MY_Controller.php.
If you are thinking it will load MY_Test_Controller.php and MY_Web_Controller.php, it won't.
If you can include multiple controllers in that one file, or include other files from that file.
You could build around this of course, but a good bit of extra information to know.

Related

Codeigniter(HMVC) core classes is not loaded in pre_contoller hook

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?

Extending CodeIgniter core classes

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

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