Welcome Guest, Not a member yet? Register   Sign In
is require_once required in CI?
#1

[eluser]mason22[/eluser]
I've made a controller that I want all my controllers to extend from. Unfortunately, all my other controllers don't have access to this controller class unless I call a require_once 'MY_Controller.php'.

Is this the only way to go about including files for my files to use?
#2

[eluser]Ben Edmunds[/eluser]
No they do. Check your default extension in config.php. And make sure you put MY_Controller in the libraries folder.
#3

[eluser]mason22[/eluser]
[quote author="Ben Edmunds" date="1261198138"]No they do. Check your default extension in config.php. And make sure you put MY_Controller in the libraries folder.[/quote]

"no they do" as in there is an easier way? I assumed there was a way for CI to automatically load it by looking at the prefix. My prefix is set to MY_ but it still comes up with errors.

this is the error that I get....

Fatal error: Class 'MY_Common' not found in /opt/lampp/htdocs/test/system/application/controllers/contact.php on line 3
#4

[eluser]Colin Williams[/eluser]
Maybe you should extend MY_Controller and not MY_Common...
#5

[eluser]mason22[/eluser]
[quote author="Colin Williams" date="1261202571"]Maybe you should extend MY_Controller and not MY_Common...[/quote]

oops sorry. my controller was called "MY_Common".
#6

[eluser]mason22[/eluser]
My code still won't work >.< I've looked at the guides and I'm having no success.

MY_Common.php looks like this:

Code:
class MY_Common extends Controller{
  function MY_Common(){
    parent::Controller();
  }
}
my common is in the library folder

contact.php looks like:

Code:
class Contact extends MY_Common{
  function Contact(){
    parent::MY_Common();
  }
  function index(){    
    $this->load->view('contact_view', $this->data);
  }
}
contact is in the controller folder. I get this error:

Fatal error: Class 'MY_Common' not found
#7

[eluser]Armchair Samurai[/eluser]
Change MY_Common.php to MY_Controller.php, but keep the content the same.
#8

[eluser]Colin Williams[/eluser]
Quote:Unfortunately, all my other controllers don’t have access to this controller class unless I call a require_once ‘MY_Controller.php’.

You threw us all off with that. I thought you had a file MY_Controller.php with a class MY_Common declared in it.
#9

[eluser]mason22[/eluser]
[quote author="Armchair Samurai" date="1261218649"]Change MY_Common.php to MY_Controller.php, but keep the content the same.[/quote]

Hey it worked!!!!!! thanks.
#10

[eluser]bikuta[/eluser]
So why did it have to be called MY_Controller.php? Is that because CI automatically loads Controller and MY_Controller (if it exists)?
So the problem would've been because he didn't specify MY_Common to be loaded?




Theme © iAndrew 2016 - Forum software by © MyBB