Welcome Guest, Not a member yet? Register   Sign In
extend MY_Controller
#1

[eluser]Zawardo[/eluser]
I created a MY_Controller class, putted into core folder and it's working fine.
Now i want to extend another time the controller, let's say MY_Second_controller.
If i put this class into core folder and declare it as class MY_Second_controller. extends MY_Controller {.....
when i try to use it with an application controller (like controller.php declared as class controller extends MY_Second_controller {...) i get an error of MY_Second_controller not found.
I have inserted into config-php a piece of code to autoload class from core folder and it's working.

My question is: why i had to add this autoload function? everything prefixed with MY_ should be autoloaded by default or do i have misunderstood anything?
#2

[eluser]theprodigy[/eluser]
Can you show your code please.
Show the basics of your MY_Controller, and MY_Second_Controller (and remember to put it within the code tags).
#3

[eluser]Zawardo[/eluser]
i'll keep it simple:
/application/core/MY_controller.php
Code:
<?php

class  MY_Controller  extends  CI_Controller  {

    function __construct() {
        parent::__construct();
        //do nothing
     }


}

/application/core/MY_LoggedController.php
Code:
<?php

class MY_LoggedController extends MY_Controller  {

    function __construct() {
        parent::__construct();
        //do nothing
     }

}

and finally a normal application controller (into /application/controllers):
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pippo extends MY_LoggedController {
    
    function __construct()
    {
        parent::__construct();
        //do nothing
    }

}
If i extend Pippo with MY_controller works fine, il i estend Pippo with MY_LoggedController i have error (not found). As i said i solved using autoload script (taken form here link). But i would like to undestand why it didn't work without...
#4

[eluser]theprodigy[/eluser]
Quote:i’ll keep it simple:
/application/core/MY_controller.php
Is this a typo, or is the file named MY_controller.php (as opposed to MY_Controller.php (with a capital C))?

Quote:i have error (not found)

What is the exact error? It can't find MY_Controller (which MY_LoggedController is extending)? Or it can't find MY_LoggedController?
#5

[eluser]Zawardo[/eluser]
[quote author="theprodigy" date="1305989257"]
Quote:i’ll keep it simple:
/application/core/MY_controller.php
Is this a typo, or is the file named MY_controller.php (as opposed to MY_Controller.php (with a capital C))?

Quote:i have error (not found)

What is the exact error? It can't find MY_Controller (which MY_LoggedController is extending)? Or it can't find MY_LoggedController?[/quote]

The first one is a typo Tongue
The second one is cant't find MY_LoggedController
#6

[eluser]Zawardo[/eluser]
nothing... bad post
#7

[eluser]InsiteFX[/eluser]
If I am not mistaken Codigniter.php only loads one MY_Controller so you should extend all your Controllers of the MY_Controller without the MY_ extension! Logged_Controller extends MY_Controller

InsiteFX
#8

[eluser]theprodigy[/eluser]
I'm not exactly sure how we are doing it at work, but we have several levels of hierarchy in the controllers. I've looked through the autoload.php, and it doesn't look like we are autoloading any of them, but have have a MY_Controller that extends CI_Controller, and then one or two controllers that extend MY_Controller, and then several controllers that extend those controllers, etc.

All I can say is they aren't being autoloaded, and there isn't any require() or include() statements going on, and most of the extended controllers are in the core folder. I'll have to double check the code to see what we are doing, but I know it's possible.

I do know that if you want to use the parent controller as a controller, you'll have to have it in the controllers directory. It's not URL accessible in the core directory. Same goes for Model. If all you are doing is extending it, then the core directory is fine. But if you want to actually use it (and load it) as a model, then it needs to be in the models directory.
#9

[eluser]InsiteFX[/eluser]
I dont know if they have fixed it but before you had to use an autoload in the config.php
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 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
#10

[eluser]Zawardo[/eluser]
[quote author="InsiteFX" date="1306017226"]I dont know if they have fixed it but before you had to use an autoload in the config.php
InsiteFX[/quote]

thank you, i already fixed using that code (i put the link in the first post), i just wanted to know how does work CI Tongue and why it's not working as i expected...




Theme © iAndrew 2016 - Forum software by © MyBB