• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want create a Typical Controller and extends all controller from it, but CI don't find

#11
[eluser]Zorancho[/eluser]
First thing, create new file at application/libraries folder and name it MY_Controller.php
If you want to change the prefix MY, go to application/config/config.php file, on line 106 you got this: $config['subclass_prefix'] = 'MY_'; which means you can add your own subclass prefix.
You need this when you extend core classes from CI.
In MY(or whatever your prefix is)_Controller.php file, create new class.
Code:
//This is parent of all of your controllers from application/controllers folder.
class MY(or whatever your prefix is)_Controller extends Controller
{
   //You will execute parent's constructor and inherit all of its methods.
   function __construct()
   {
      parent::__construct();
   }

   //All methods here can be used in your controllers like: $this->sharedFunction();
   public function sharedFunction()
   {
      echo 'I am available in all of your controllers';
   }
}


//You can create one parent for the front controllers.
class Front_Controller extends MY(or whatever your prefix is)_Controller
{
   public function __construct()
   {
      parent::__construct();
   }
  
   public function frontFunction()
   {
      echo 'I am available in all of your front controllers';
   }
  
}

//One for the admin part in the same file.
class Admin_Controller extends MY(or whatever your prefix is)_Controller
{
   public function __construct()
   {
      parent::__construct();
   }
  
   public function adminFunction()
   {
      echo 'I am available in all of your admin controllers';
   }
  
}

Then in your application/controllers/home.php
Code:
class Home extends Front_Controller
{
   public function __construct()
   {
      parent::__construct();
   }

   public function index()
   {
      $this->frontFunction();
   }
}

In your application/controllers/admin/dashboard.php
Code:
class Dashboard extends Admin_Controller
{
   public function __construct()
   {
      parent::__construct();
   }

   public function index()
   {
      $this->adminFunction();
   }
}
Hope it helps

#12
[eluser]Kaosland[/eluser]
Thanks
It's a good exemple.
But a don't understand why, i have insert a line befort _autoload then work now.!!!!!
But thanks again

#13
[eluser]Zorancho[/eluser]
Why would you want to autoload controllers? That's done in the Routing of CI according to the URL you are executing through the browser. In MVC structure such as CI, usually models could be auto loaded, but it is not necessary at all and auto loading controllers is a bad practice i think, someone please correct me if i am wrong.

#14
[eluser]Kaosland[/eluser]
Because I have create a ControlerFront with multi lib that i had create for create&management;Background of the Front;.
So i can recopy the code then what POO exits.
My other controler extend from Controlerfront.
SO the pobleme it'ss next
when CI load controlle thenews extend controlerFont, CI don't find controlerFont.
Thanks for you help.

#15
[eluser]Zorancho[/eluser]
Then, please copy the error(s) you are getting and the code causing the errors, cause like this i really cannot help you.

#16
[eluser]Kaosland[/eluser]
I solve error
I have intoduce a blank line befor
Code:
//blank
function __autoload($class)
{
    if (file_exists(APPPATH."controllers/".$class.EXT))
    {
        require_once(APPPATH.'controllers/'.$class.EXT);
require_once(APPPATH.'controllers/'.strtolower($class).EXT);
    }
}
then now work
i don't understand why but work

#17
[eluser]Phil Sturgeon[/eluser]
I wrote an article to explain how to do this properly. nothing new, but clearer than the random tit-bits posted on various topics.

http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY

#18
[eluser]Kaosland[/eluser]
Thanks for you.
It's a good sum up.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.