CodeIgniter Forums
Check method/function exist with Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Check method/function exist with Controller (/showthread.php?tid=511)



Check method/function exist with Controller - vrsotto - 12-11-2014

Hi guys,

can anyone please help, how can i check if method/funcion exist within class/controller? say:

PHP Code:
class User extends CI_Controller {
 
   public function __construct() {
 
     parent::__construct();
 
   }

 
   public function login() {
 
      ...
 
   }

the idea is, i want to check function "login()" if exist and execute.

hope someone could help.

Thanks in advanced 


RE: Check method/function exist with Controller - trentramseyer - 12-11-2014

Depending where in the application you want this to happen, you can use the _remap function from CI
https://ellislab.com/codeigniter/user-guide/general/controllers.html


PHP Code:
public function _remap($method$params = array())
{
    
$method 'process_'.$method;
    if (
method_exists($this$method))
    {
        return 
call_user_func_array(array($this$method), $params);
    }
    
show_404();




RE: Check method/function exist with Controller - sv3tli0 - 12-12-2014

What is your idea ?
Where you want to make this check?


RE: Check method/function exist with Controller - Rufnex - 12-12-2014

You can use the _remap method from CI as described here:

http://www.codeigniter.com/userguide3/general/controllers.html#remapping-method-calls

The following short example shows you a quick usage

PHP Code:
class User extends CI_Controller {
    public function 
__construct() {
      
parent::__construct();
    }

    function 
_remap($method)
    {
        if(
method_exists($this,$method))
        {
            
call_user_func(array($this$method));
            return 
false;
        }
        else
        {
            echo 
'Method not found!';
        }
    }

    
// .... your methods ....





RE: Check method/function exist with Controller - vrsotto - 12-14-2014

Ahh, thank you guys. very much appreciated all your help.