Welcome Guest, Not a member yet? Register   Sign In
Frontend and Backend (Base controllers)
#1

[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?


#2

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

[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.
#4

[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_ .
#5

[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
#6

[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.

#7

[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...
#8

[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();  
   }
}
#9

[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.
#10

[eluser]cPage[/eluser]
Tuts+




Theme © iAndrew 2016 - Forum software by © MyBB