Welcome Guest, Not a member yet? Register   Sign In
Can't extend CI_Controller
#1

[eluser]Lukas1[/eluser]
Hello, I am using CodeIgniter 2.0 and I am new to the framework. I tried extending the CI_Controller class and it works for me on my computer using Wamp. But when I upload my project on other servers, I get this fatal error:
Fatal error: Class 'MY_Controller' not found in [PATH]

I don't understand why is this happening, my extended controller is in /core folder(not /libraries), the prefix is correct and it works on my computer, so I don't know, why it doesn't work on other servers(I tried 2 different servers). Does anyone have any idea, what could be my problem?
#2

[eluser]InsiteFX[/eluser]
Check to see if your server is lowercase sensitive if so then all filenames need to converted to lowercase!

InsiteFX
#3

[eluser]Lukas1[/eluser]
my filename is lowercase.. I tried to rename the file to MY_Controller.php with no success
#4

[eluser]InsiteFX[/eluser]
1) Did you change your index.php file to point to where your system and application folders are located?
2) Did you change application/config/config.php to the correct base_url ?

If you have an .htaccess file try removing it!

InsiteFX
#5

[eluser]Lukas1[/eluser]
Hello,

1)I haven't changed the index.php file, but since I didn't change anything at all with the folder structure, I don't think this is the issue. If I indeed should have changed something in index.php, this should have been documented and I haven't found anything like this mentioned in documentation. If such thing would affect extending core classes, this should have been documented in the extending core libraries section(now it is not)
2)Yes, I've done that, but I don't see, how that is related, that's only URL and URL is not the deciding factor in finding path to core classes
3) I don't use .htaccess file for now, since I am only starting with my project, playing with CI etc...

I have pretty default setup, I mean, I really haven't done anything 'unusual' (if such term can be applied for trying out a php framework), I pretty much followed basic documentation, and it did work for me on my computer, so I really am not sure, what could possibly prevent loading my extended controller. I don't know, how is my extended controller being included(or how it should be included), if CI uses some kind of advanced php functionality, or if it is really only require/include functions...
#6

[eluser]InsiteFX[/eluser]
Controller now found in path is telling you it cannot find the directory and controller name!

The base_url does pertain to finding your classes etc!

Code:
// leave blank and CodeIgniter will try to figure it out!
$config['base_url']    = '';

// else set it!
$config['base_url']    = 'http://www.your_site.com/';

// for example my site is installed in public_html
$config['base_url']    = 'http://www.mysite.com/public_html/';

Also you my need to upgrade to the CI 2.0.1 in the Reactor!

InsiteFX
#7

[eluser]Lukas1[/eluser]
Hello, thanks for answer. I don't use CI Reactor, I use the stable version of CI.

I am not sure, you understand my problem well, after reading your answer. It seems to me, you think, that I am not able to create a Controller. This is not the case, I don't have any problems with that. I want to extend the core CI_Controller however, and let my controllers extend not the CI_Controller, but my extended Controller.

So for example, let's say, I want to have a Test_controller. If I would want to just create a controller I can do this
Code:
public class Test_controller extends CI_Controller {
    public function index() {
      //do something
    }
  }

This approach works for me. And this is, where URL is involved.

However I want for example to have every of my controller a function that will return some concrete value.. I don't know, for example HTML header.

So I want to do this:
in application/core/my_controller.php
Code:
public class MY_Controller extends CI_Controller {
    protected function get_html_header() {
      return '<head><title>Title of this page</title><'head>';
    }
in application/controller/test_controller.php
Code:
public class Test_controller extends MY_Controller {
    public function index() {
      $htmlHeader = $this->get_html_header();
      //do something
    }
  }

When I run this on my computer with Wamp Server, it works just as documented. However after uploading my CI project with this code, I get error:
Fatal error: Class ‘MY_Controller’ not found in [PATH]/application/controller/test_controller.php

So URL is correct, it's not a problem of wrong URL, it has absolutely nothing to do with it.

For now I have to manualy include my_controller.php to every of my controllers, if I want to have my project run on the server. I don't know why is this the case, why including application/core/my_controller.php fails...
#8

[eluser]InsiteFX[/eluser]
Your MY_Controller needs to be in application/core not application/controllers !

If the CI Library is in system/core then the extended Libraries go into application/core

If the CI Library is in system/libraries the the extended Libraries go into application/libraries

InsiteFX
#9

[eluser]Lukas1[/eluser]
[quote author="InsiteFX" date="1302032125"]Your MY_Controller needs to be in application/core not application/controllers !

If the CI Library is in system/core then the extended Libraries go into application/core

If the CI Library is in system/libraries the the extended Libraries go into application/libraries

InsiteFX[/quote]
I really appreciate, that you are trying to help me, thank you, but do you even read, what I am writing here all the time? I repeated at least twice, that MY_Controller IS in application/core. And that it should be there and not in libraries folder, since I am using CI 2.0 and haven't changed anything with folder structure. Let me now quote my previous post here:
[quote author="Lukas1" date="1302004787"]
So I want to do this:
in application/core/my_controller.php
Code:
public class MY_Controller extends CI_Controller {
    protected function get_html_header() {
      return '<head><title>Title of this page</title><'head>';
    }
[/quote]
The very post, you have replied to.
I am not sure, if my english is incomprehensible, or what the problem is, since you haven't understood what my problem is from the very beginning. I am sorry for pointing this out, but it really disappointed me... Don't get me wrong, I really appreciate your willingnes to help me.

Anyway I already found the solution for my problem. I've digged into the CI code and found, that CI searches for a file called MY_Controller.php (my file was called my_controller.php and then I tried MY_controller.php, but with no success->everything because of C in the word controller was lowercase, when CI expected it to be uppercase). So it was my mistake, I haven't read the documentation carefully.

Notice for everyone:
If anyone has a problem with extending core classes, like for example Controller, don't forget that the filename must have the first three letters uppercase->MY_Controller.php, not my_controller.php, or MY_controller.php.
#10

[eluser]InsiteFX[/eluser]
And what part did you not read above?

Your MY_Controller needs to be in application/core not application/controllers !
-------------

CodeIgniter User Guide!
Your new class name and filename must be prefixed with MY_

Which tell's me that you did not read the CodeIgniter User Guide.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB