Welcome Guest, Not a member yet? Register   Sign In
Using Hooks
#1

Hi,

I'm trying to use my own classes with hooks, so on a fresh install of last CI, I've enabled hooks in config file.
Modified hooks.php by adding :
PHP Code:
$hook['pre_system'][] = array(
 
 'class' => '',
 
 'function' => 'load_app_controllers',
 
 'filename' => 'App_controllers.php',
 
 'filepath' => 'hooks'
); 


Added a file call App_Controllers.php in hooks folder :

PHP Code:
function load_app_controllers()
{
 
 spl_autoload_register('my_own_controllers');
}
function 
my_own_controllers($class)
{
 
 if (strpos($class'CI_') !== 0)
 
 {
 
   if (is_readable(APPPATH 'core/' $class '.php'))
 
   {
 
     require_once(APPPATH 'core/' $class '.php');
 
   }
 
 }




Then a file MY_Controller.php in Controllers :
PHP Code:
<?php

class MY_Controller extends CI_Controller
{
 
 public function __construct()
 
 {
 
   parent::__construct;
 
 }
}

class 
Admin_Controller extends MY_Controller
{
 
 function __construct()
 
 {
 
   parent::__construct();
 
   echo 'This is from admin controller';
 
 }
}

class 
Public_Controller extends MY_Controller
{
 
 function __construct()
 
 {
 
   parent::__construct();
 
   echo 'This is from public controller';
 
 }



And two file in controllers "Admin_Controller.php" :
PHP Code:
<?php

class Admin_Controller extends MY_Controller
{
 
 function __construct()
 
 {
 
   parent::__construct();
 
   echo 'This is from admin controller';
 
 }

Public_Controller.php :
PHP Code:
<?php

class Public_Controller extends MY_Controller
{
 
 function __construct()
 
 {
 
   parent::__construct();
 
   echo 'This is from public controller';
 
 }



And finally modified Welcome.php :
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends Admin_Controller 

Problem is that I got error :
Code:
Fatal error: Class 'Admin_Controller' not found in /Applications/MAMP/htdocs/hooks/application/controllers/Welcome.php on line 4
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/hooks/application/controllers/Welcome.php:4)

Filename: core/Common.php

Line Number: 578

Backtrace:

A PHP Error was encountered

Severity: Error

Message: Class 'Admin_Controller' not found

Filename: controllers/Welcome.php

Line Number: 4

Backtrace:

Someone ?
Reply
#2

If you have MY_Controller in the application/core folder you don't need the hook CI will use it be default as long as the it is define in config.php $config['subclass_prefix'] = 'MY_'; (default)

Since you have your Admin and Public controller defined in the MY_Controller file, you don't need these 2 controller files.
A good decision is based on knowledge and not on numbers. - Plato

Reply
#3

Code for the MY_Controller:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class Base_Controller
 * 
 * This file should be saved at:
 * ./application/core/MY_Controller.php
 * 
 */
class Base_Controller extends CI_Controller {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * @var  array
     */
    
protected $data = array();
    
    
/**
     * --------------------------------------------------------------------
     *  __construct
     * --------------------------------------------------------------------
     *
     * Class Constructor    PHP 5+
     *
     * @access    public
     */
    
public function __construct()
    {
        
parent::__construct();

        
/**
         * check to see if we want to use the CI profiler.
         * Requries the below in the ./application/config/config.php
         * //$config['profiler'] = TRUE;
         * $config['profiler'] = FALSE;
         */
        
$this->output->enable_profiler($this->config->item('profiler'));

        
log_message('debug''CI: MY_Controller class loaded');
    }

}    
// End of Base_Controller Class.

/**
 * Class Admin_Controller
 */
class Admin_Controller extends Base_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */
    
    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }


}    
// End of Admin_Controller Class.

/**
 * Class Public_Controller
 */
class Public_Controller extends Base_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */
    
    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }

}    
// End of Public_Controller Class.

/**
 * ------------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * ------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Sorry as this was my first post and it was moderate, I could not reply earlier, I ended up solving my problem, the MY_Controller was in wrong place  Big Grin.
Thx for your replies, I've understood how it works now Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB