Welcome Guest, Not a member yet? Register   Sign In
CI 2 - extending the controller problems
#1

[eluser]richzilla[/eluser]
hi all,
ive poked around the user guide and the forums for an answer to this but i cant seem to find it. Im trying to extend the default controller class in CI 2:

So far, i have in application controllers:

Code:
<?php

/**
* Controller for app home page
*/


class Home extends MY_Controller {

    function __construct()
    {
        parent::MY_Controller();    
    }
    
    function index()
    {
        $this->load->view('memo_home.php');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/home.php *

and in application/core in a file call MY_Controller.php:

Code:
<?php

class MY_Controller extends Controller{
    
    function __construct()
    {
        parent::Controller();
        echo "Parent has been called";
    }
    
}

?>

yet when i run the system i get an error:

Code:
Class 'MY_Controller' not found

ive checked my config file and the prefix is set to "MY_" and all of the file names are correct. Can anybody help me out?
#2

[eluser]kaejiavo[/eluser]
hi ricardino,

try changing the parent constructor in your controller:
Code:
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
...

Marco
#3

[eluser]richzilla[/eluser]
[quote author="mawi27" date="1286795721"]hi ricardino,

try changing the parent constructor in your controller:
Code:
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
...

Marco[/quote]

No luck im afraid, thanks for the help.
#4

[eluser]InsiteFX[/eluser]
Code:
// this should be in application/controllers
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {
        $this->load->view('memo_home.php');
    }
}

Code:
// this should be in application/core
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {

    }

}

If this will not work check to make sure that your default controller
is Home and that you have the view file named memo_home.php.

I just tested this on my system and it works fine!

InsiteFX
#5

[eluser]eddel[/eluser]
[quote author="InsiteFX" date="1286884295"]
Code:
// this should be in application/controllers
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {
        $this->load->view('memo_home.php');
    }
}

Code:
// this should be in application/core
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {

    }

}

If this will not work check to make sure that your default controller
is Home and that you have the view file named memo_home.php.

I just tested this on my system and it works fine!

InsiteFX[/quote]

I am a bit confused with your answer. In the above sample, the first controller should be located in the application/controller, and the other is in application/core, but the two controllers are the same :question: :long:
#6

[eluser]InsiteFX[/eluser]
The two controllers are not the same the MY_Controller is extending the core CI_Controller
so it has to go into application/core !

If the Class you are extending is in the system/core then it goes in application/core
If the Class you are extending is in the system/libraries then it goes in application/libraries

Also you need this or the MY_Controller will not load from application/core
Autoload by Phil
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon. New Version!
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will SPL_Autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}

InsiteFX
#7

[eluser]eddel[/eluser]
yes then it should be

// this should be in application/core
Code:
class MY_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();    
    }
}

// this should be in application/controllers
Code:
class Home extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }
    
    function index()
    {
        $this->load->view('memo_home.php');
    }
}


:question: :exclaim:




Theme © iAndrew 2016 - Forum software by © MyBB