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

[eluser]blasto333[/eluser]
Is it necessary to have all Controllers that override Controller in application/libraries/MY_Controller.php?

I know this way they will be autoloaded, but what is the harm in doing this:

Currently in MY_Controller I have this:

class Secure_Area extends Controller
{
..
..
..
}

abstract class Person_Controller extends Secure_Area implements iPerson_Controller
{
..
..
..
}

For readability I would rather have two separate files:
libraries/Secure_Area.php
libraries/Person_Controller.php
#2

[eluser]blasto333[/eluser]
I just ended up making separate files and then doing a require_once inside MY_Controller.php
#3

[eluser]Frank Berger[/eluser]
you should seriously consider to read and understand CI's documentation. You don't need a MY_controller at all unless you want to extend the controller class in general. All your controller files go into /application/controllers/ and do not need a special include or any other stuff like that.

Read this chapter and all its sub-chapters in the documentation:
http://ellislab.com/codeigniter/user-gui...llers.html

Frank
#4

[eluser]blasto333[/eluser]
I have read it, I am extending the controller class in general, thats why they were in MY_Controller.php to begin with. The controller classes in this file are not used in the application as a normal controller would. These are just base controller classes that the real controllers extend.

It might make more sense if I show you the full code:

MY_Controller.php
Code:
<?php
require_once ("Secure_Area.php");
require_once ("Person_Controller.php");
?>

Secure_Area.php
Code:
<?php
class Secure_Area extends Controller
{
    /*
    Controllers that are considered secure extend Secure_Area, optionally a $module_id can
    be set to also check if a user can access a particular module in the system.
    */
    function __construct($module_id=null)
    {
        parent::__construct();    
        
        if(!$this->Employee->is_logged_in())
        {
            redirect('login');
        }
        
        if(!$this->Employee->has_permission($module_id,$this->Employee->get_logged_in_employee_info()->person_id))
        {
            redirect('no_access/'.$module_id);
        }
        
        //load up global data
        $logged_in_employee_info=$this->Employee->get_logged_in_employee_info();
        $data['allowed_modules']=$this->Module->get_allowed_modules($logged_in_employee_info->person_id);
        $data['user_info']=$logged_in_employee_info;
        $this->load->vars($data);
    }
}
?>

Person_Controller.php
Code:
<?php
require_once ("interfaces/iPerson_Controller.php");
abstract class Person_Controller extends Secure_Area implements iPerson_Controller
{
    function __construct($module_id=null)
    {
        parent::__construct($module_id);        
    }
    
    /*
    This returns a mailto link for persons with a certain id. This is called with AJAX.
    */
    function mailto()
    {
        $people_to_email=$this->input->post('ids');
        
        if($people_to_email!=false)
        {
            $mailto_url='mailto:';
            foreach($this->Person->get_multiple_info($people_to_email)->result() as $person)
            {
                $mailto_url.=$person->email.',';    
            }
            //remove last comma
            $mailto_url=substr($mailto_url,0,strlen($mailto_url)-1);
            
            echo $mailto_url;
            exit;
        }
        echo '#';
    }
    
    /*
    Gets one row for a person manage table. This is called using AJAX to update one row.
    */
    function get_row()
    {
        $person_id = $this->input->post('person_id');
        $data_row=get_person_data_row($this->Person->get_info($person_id),$this);
        echo $data_row;
    }
        
}
?>
#5

[eluser]Frank Berger[/eluser]
ok, makes more sense now. I had/have the same problem with the phpgACL implementation i posted in the forum here as well. The problem is that the autoload comes too late.

You're right in this case a my_controller with an include is my 'cheap' alternative as well. The only other option you have is to look into the hook functionality. There you can execute commands very early in CI's life-cycle, and within such a function you can include a custom controller class as well (or more than one)

hope that helps

Frank
#6

[eluser]Rick Jolly[/eluser]
In the name of transparency, I just require/include parent classes in the child class. It's self documenting. I don't use MY_Controller because, as you wrote, if you want more than one parent controller, you have to hide them away in the same file. Alternatively, if using class naming conventions, you could write a simple php 5 __autoload() function.
#7

[eluser]blasto333[/eluser]
Thats what I ended up doing, but didn't use __autoload as I think being verbose and transparent makes it a lot easier to understand. (Just like you said)

[quote author="Rick Jolly" date="1224452562"]In the name of transparency, I just require/include parent classes in the child class. It's self documenting. I don't use MY_Controller because, as you wrote, if you want more than one parent controller, you have to hide them away in the same file. Alternatively, if using class naming conventions, you could write a simple php 5 __autoload() function.[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB