Welcome Guest, Not a member yet? Register   Sign In
Check method/function exist with Controller
#1

(This post was last modified: 12-11-2014, 09:07 PM by vrsotto.)

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 
Reply
#2

Depending where in the application you want this to happen, you can use the _remap function from CI
https://ellislab.com/codeigniter/user-gu...llers.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();

Reply
#3

What is your idea ?
Where you want to make this check?
Best VPS Hosting : Digital Ocean
Reply
#4

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

http://www.codeigniter.com/userguide3/ge...thod-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 ....



Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB