1

I have the following simplistic code:

// FILE: controllers/Top.php

class Top extends MY_Public_Controller {
        function __construct() {
   }
   public function Top() {
        echo 'Hello';
   }
 }

// FILE: application/core/MY_Public_Controller.php

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

// FILE: application/core/MY_Controller.php

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

And I get the following the following error:

 Fatal error: Class 'MY_Public_Controller' not found in 
 /var/www/example.com/public_html/application/controllers/Top.php on line 5
 A PHP Error was encountered
 Severity: Error
 Message: Class 'MY_Public_Controller' not found
 Filename: controllers/Top.php
 Line Number: 5
 Backtrace:

Any help would be much appreciated!

2 Answers 2

1

Instead of you create a new file (MY_Public_Controller.php) to create the class My_Public_Controller.

Insert this class inside the My_Controller.php file.

In that way the My_Controller.php file will be like:

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

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

After I see another answer

Or you can make something like #Hikmat Sijapati said, but instead of you put the require_once, inside the My_Controller.php. Try to put it in the My_Public_Controller.php using 'My_Controller.php' as parameter. Something like that:

My_Public_Controller.php:

include_once('My_Controller.php');
class MY_Public_Controller extends MY_Controller {
        function __construct() {
               parent::__construct();
        }
 }

I have not tried it that way, but I think it will work.

Sign up to request clarification or add additional context in comments.

Comments

1

Try like this...

  1. You can create any number of controller but create controller's must be included in the controller that extends CI_Controller.As Below:

  2. Controller's Name and function Name Keep different (Good Way)

MY_Controller:application/core/MY_Controller.php

 class MY_Controller extends CI_Controller {
        function __construct() {
               parent::__construct();
        }
include_once('MY_Public_Controller.php');// include here
 }

MY_Public_Controller: application/core/MY_Public_Controller.php

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

And Top: application/Top.php

class Top extends MY_Public_Controller {
        function __construct() {
   }
   public function index() {  //function name must be different than controller's name
        echo 'Hello';
   }
 }

5 Comments

None of the above 2 solutions work as presented. Same error.
I tried it like this and it still doesn't work. snag.gy/S0qPYR.jpg
also include your private and admin controllers.
let's get this working first. MY_Controller -> MY_Public_Controller -> Top
are you using MX controller for HMVC?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.