Redirect to default method if CodeIgniter method doesn't exists. Redirect to default method if CodeIgniter method doesn't exists. codeigniter codeigniter

Redirect to default method if CodeIgniter method doesn't exists.


Yes there is a solution. If you have Components controller and the flilename components.php. Write following code...

<?phpif (!defined('BASEPATH'))    exit('No direct script access allowed');class Components extends CI_Controller{    public function __construct()    {        parent::__construct();    }    public function _remap($method, $params = array())    {        if (method_exists(__CLASS__, $method)) {            $this->$method($params);        } else {            $this->test_default();        }    }    // this method is exists    public function test_method()    {        echo "Yes, I am exists.";    }    // this method is exists    public function test_another($param1 = '', $param2 = '')    {        echo "Yes, I am with " . $param1 . " " . $param2;    }    // not exists - when you call /compontents/login    public function test_default()    {        echo "Oh!!!, NO i am not exists.";    }}

Since default is PHP reserved you cannot use it so instead you can write your own default method like here test_default. This will automatically checks if method exists in your class and redirect accordingly. It also support parameters. This work perfectly for me. You can test yourself. Thanks!!