Welcome Guest, Not a member yet? Register   Sign In
CREATING OWN CONTROLLERS
#1

[eluser]Unknown[/eluser]
Hi I'm trying to create a new controller extending from CI_Controller. My problem starts here. When I try to put the name to that controller, the only name that the system accepts is MY_Controller.
I have one file called MY_ControllerAA.php with this code in Application/core
Code:
<?php
class MY_ControllerAA extends CI_Controller {
    protected $photos;

    public function __construct()
    {
        parent::__construct();
        $this->photos = 0;
    }

    public function set_photos($number){
        $this->photos = $number;
    }

    public function get_photos(){
        return $this->photos;
    }
}

And I have another file in application/controllers called welcome.php with this code
Code:
<?php
class Welcome extends MY_ControllerAA {

    function __construct()
    {
        parent::__construct();
        $this->set_photos(3);
    }

    function index()
    {
        echo $this->get_photos();
    }
}

It's only a test code but when I try to get the index function I get this error

Fatal error: Class 'MY_ControllerAA' not found in /Applications/MAMP/htdocs/mallorcadiving/intranet_mallorcadiving/application/controllers/welcome.php on line 2

When I change the name of my class to MyController it works fine. I don't know why it is. Could someone help me?

Lots of thanks
#2

[eluser]jairoh_[/eluser]
MY_Controller should be put into the core folder. IMHO, you should not use other names, i think CI restricts it.
and in your autoload.php add this one to the top
Code:
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
  {
    @include_once( APPPATH . 'core/'. $class . EXT );
  }
}
#3

[eluser]Unknown[/eluser]
But what about if I wanted to extend two different controllers. How can I put another name to the second one? . Moreover, it works for me without modifying the autoload.php file.

Do you know how can I do that
Thank you
#4

[eluser]jairoh_[/eluser]
[quote author="sergitome" date="1380003950"]But what about if I wanted to extend two different controllers. How can I put another name to the second one? . Moreover, it works for me without modifying the autoload.php file.

Do you know how can I do that
Thank you[/quote]

haven't tried that sir. Smile




Theme © iAndrew 2016 - Forum software by © MyBB