CodeIgniter Forums

Full Version: How to change the method called from class constructor ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hi !

This is my very first post on here, I'm new to codeigniter !

I found a lor of answers to my newby issues, but one :

i call the url customer/first_called , but a switch in the constructor triggers a call to another function constructor_called() instead, and i don't want first_called() to be called anymore.

Code:
class Customer extends MY_Controller {
    var $user_type;
    function __construct()
    {
        parent::__construct();
        
        $condition = TRUE;
        
        if($condition == TRUE)
            return $this->constructor_called();
            
        
    }

function first_called()
{
//do something
}

function constructor_called()
{
//do something else
}
}


If I do like this, both functions will be called : constructor_called() then first_called()

How can i prevent first_called() to be called?

Thanks for any tip,

cheers !

Nicolas (France)

El Forum

[eluser]wh1tel1te[/eluser]
Hi ndavoust,

I don't think it is possible to do this sort of thing in CI (calling methods from the constructor without triggering the currently called method). Try the following code, then call the URL customer/

Code:
class Customer extends MY_Controller {
    var $user_type;
    function __construct()
    {
        parent::__construct();
    }

function index()
{
        $condition = TRUE;
        
        if($condition == TRUE)
            return $this->constructor_called();
        else
            return $this->first_called();
}

function first_called()
{
//do something
}

function constructor_called()
{
//do something else
}
}

El Forum

[eluser]WanWizard[/eluser]
To catch the called method and do custom routing within the controller, use the _remap() method.

See the user guide: http://ellislab.com/codeigniter/user-gui...#remapping (it is an excellent read, and a must before you start with CI!)

El Forum

[eluser]Unknown[/eluser]
Thanks WanWizard ! This was exactly what I needed and it works like a charm !

Great to start the day by learning something new and see my project go one step ahead !

:-)