CodeIgniter Forums

Full Version: Frontend and Backend (Base controllers)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]cPage[/eluser]
Is it ok to put this code in config.php ?

Code:
function __autoload($classname)
{
if( strpos($classname, 'CI_') !== 0 )
{
  $file = APPPATH . 'libraries/' . $classname . '.php';
  if(file_exists($file) && is_file($file))
  {
   @include_once($file);
  }
}
}

I saw this little hack in a tutorial about managing Frontend_controller and Backend_controller.
Quote:CI_Controller
-MY_Controller (core/)
-Frontend_Controller (libraries/)
-Backend_Controller (libraries/)

credit to : Tuts+

I tried to skip the function of the config file by adding those 2 controllers to the autoload array without success, and then i understood that MY_ is the only way, that i can implent my own libraires, or this code in config.php.

It would be nice if CI add something else than MY_ other options like Frontend or Backend .

Is there something more appropriate for Frontend and Backend supports?


El Forum

[eluser]xtremer360[/eluser]
Nope. That works just fine. I use that for my applications.

El Forum

[eluser]Aken[/eluser]
What exactly is the point of this autoload? If you're just trying to create two different base controllers, you can do that just fine inside a single MY_Controller.php file. You don't have to name the class MY_Controller, so long as the filename is named as such. You could have 10 different base classes in there if you wanted.

El Forum

[eluser]cPage[/eluser]
[quote author="Aken" date="1358747741"]What exactly is the point of this autoload? If you're just trying to create two different base controllers, you can do that just fine inside a single MY_Controller.php file. You don't have to name the class MY_Controller, so long as the filename is named as such. You could have 10 different base classes in there if you wanted.[/quote]

To keep the code DRY. By this way all common functions for both backend and frontend will be in MY_ .

El Forum

[eluser]PhilTem[/eluser]
Nope, that's not what @Aken suggested. He was suggesting to create a file MY_Controller, which holds multiple classes e.g.

Code:
<?php

class MY_Controller extends CI_Controller {}

class Frontend_Controller extends MY_Controller {}

class Backend_Controller extends MY_Controller {}
// and so on and so forth

That's got nothing to do with DRY code, it's even better than having let's say 20 class files in APPPATH . 'core/' Wink

The advantage of this over the __autoload function: Once the MY_Controller file got included by the Loader class, all other classes can be instantiated easily and directly without even using a __autoload function thus kinda speeding up the application, as well Wink

El Forum

[eluser]cPage[/eluser]
Quote:That's got nothing to do with DRY code, it's even better than having let's say 20 class files in APPPATH . 'core/' Wink

Where do you see 20 class files in the above pattern ? There's only 1 in core/MY_Controller

Other files are in libraries.

But i got the point anyway, thanks.

El Forum

[eluser]Aken[/eluser]
If you're only loading a single MY_Controller core file, and the rest are libraries, then I really don't see the need for the autoload. Libraries are loaded via the Loader class already. The only way you'd need autoloading is if you wanted to use the "new Class()" instantiation method, which is completely unrelated to base MY_Controller...

El Forum

[eluser]cPage[/eluser]
[quote author="Aken" date="1358812731"]If you're only loading a single MY_Controller core file, and the rest are libraries, then I really don't see the need for the autoload. Libraries are loaded via the Loader class already. The only way you'd need autoloading is if you wanted to use the "new Class()" instantiation method, which is completely unrelated to base MY_Controller...[/quote]

Because you can't extend the base MY_Controller into libraries like this :

Code:
class Frontend  extends  MY_Controller { ... }

without using the autoload in the config file. I think it depends on what you can stand. I can't stand many classes in 1 file. But i can stand a simple function() in config file, that gives the choice to put every base classes into librairies/ . And using a powerful child such as :

Code:
admin extends Admin_Controller()
{
   function  __construct()
   {
        parent::__construct();  
   }
}

El Forum

[eluser]Aken[/eluser]
[quote author="cPage" date="1358816404"]Because you can't extend the base MY_Controller into libraries like this :

Code:
class Frontend  extends  MY_Controller { ... }
[/quote]

I don't know if you're confusing terminology, but you never want libraries to extend a controller class.

If you want to avoid multiple classes in the same file, then just use normal php and include/require the files from your MY_Controller.php file. There is no reason to create an autoload and potentially add problems to your code in the future so you can support two files, when you can just toss in some normal PHP.

El Forum

[eluser]cPage[/eluser]
Tuts+
Pages: 1 2