• 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

#1
[eluser]Kaosland[/eluser]
Hello

I create a controller with fonction i call frontcontroller beccause many page don't change almost.

Class FrontController exends Controller{

function __construct() {...}

function 1;
function 2;
function 3;


}

I have place un controller rep, i tested un lib rep with $this->loadlib...

but when I want create one controller exends FrontController, i have error Fatal error: Class 'FrontController' not found.

So someone can tell me how i do for working.

#2
[eluser]Cro_Crx[/eluser]
You need to create a MY_Controller class which extends Controller. From there all classes will be able to extend it. You can't just write any class and hope that CI can find it as only the required controller is automatically loaded according to the filename. In your case CodeIgniter doesn't know where the FrontController class is when its looking to extend it.

Here's some more info: http://ellislab.com/codeigniter/user-gui...asses.html

#3
[eluser]Kaosland[/eluser]
Thanks.

I had testing that but don't work, atal error: Class 'CI_Controller' not found .
PS with MY_*** I can create only typical class, But I want a controller for Front and a controller for back so 2 controller. (MY_MY ?)

#4
[eluser]Cro_Crx[/eluser]
Make sure that your MY_Controller is extending CI_Controller and your other classes extend MY_Controller. You'll get that error if one of your regular controllers tries to extend CI_Controller instead of its alias 'Controller'.

If you want to achieve this functionality, you can do it with a single MY_Controller class. Simply add a function to use if it's an admin page. Something like this for the Base Controller:

Code:
Class MY_Controller extends CI_Controller
{
    function MY_Controller()
    {
        parent::Controller();
        /* Perform Actions here that All Controllers will need. */

    }

    function require_admin()
    {
        /* Perform actions here required for using admin pages. */
    }

  
}

Then on your Admin Controller do something like this:

Code:
Class Admin extends MY_Controller
{
    function Admin()
    {
        parent::MY_Controller();
        $this->require_admin(); /* This makes sure our user has admin privileges. All functions within this controller will now run this.*/
    }

}

#5
[eluser]Unknown[/eluser]
It seems that, the code doesn't work...It needs a little changes for it...but I like the topic because it is very useful but not so familiar to me...

#6
[eluser]n0xie[/eluser]
[quote author="Kaosland" date="1264665649"]
So someone can tell me how i do for working.[/quote]

Have you tried this ?

#7
[eluser]Cro_Crx[/eluser]
[quote author="Mic2x" date="1264693315"]It seems that, the code doesn't work...It needs a little changes for it...but I like the topic because it is very useful but not so familiar to me...[/quote]

Oh yeah, sorry the line:

Code:
parent::Controller();

Should be this instead:

Code:
parent::CI_Controller();

#8
[eluser]Kaosland[/eluser]
Thanks

But when i use exends CI_controller have again non found CI_contoller.

But I use the solution of n0xie that work, and I prefer because I can
do

FrontController extends controller and AdminController extends controller

Thank everyone for your help

#9
[eluser]Phil Sturgeon[/eluser]
Actually you should be using:

Code:
parent::MY_Controller();
// or
parent::FrontController();

Combine that with the __autoload function and you are good to go.

#10
[eluser]Kaosland[/eluser]
RE
For my dev in localhost
using
function __autoload($class)
{
if (file_exists(APPPATH."controllers/".strtolower($class).EXT))
{
require_once(APPPATH.'controllers/'.strtolower($class).EXT);
}
}

Work.
But online i have
can't redefien _autoload.
somebody can help me.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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