开发者

how to call a function in another controller in code igniter?

开发者 https://www.devze.com 2023-01-26 09:54 出处:网络
I want to call a function in another controller. for example if user try to log in with incorrect parameter then the application will redirect to another controller and passing a variable (array).

I want to call a function in another controller. for example if user try to log in with incorrect parameter then the application will redirect to another controller and passing a variable (array).

class User extends Controller {

function User()
{
   parent::Controller();
}

function doLogin()
{
  $userData = $this->users->getAuthUserData($user,$password);
  if(empty($userData)){ 
   /开发者_C百科/ this is where i need to call a function from another controller
  }else{
   echo 'logged in';
  }
}

}

is it possible passing a variable using redirect() function in url helper?


Yes you can use redirect('othercontroller/function/'.url_encode($data), 'location');

That should work.

edit: you could also put the code in a helper.


<?php    
$array = array('foo'=>'bar', 'baz'=>'fubar', 'bar' => 'fuzz');

    $json = json_encode($array);

    $encoded_json= urlencode($json);
    /* now pass this variable to your URL redirect)

    /* on your receiving page:*/
    $decoded_json= urldecode($encoded_json);

    /* convert JSON string to an array and output it */
    print_r(json_decode($decoded_json, true));
?>

this code:

takes an array, converts it to a JSON encoded string.

we then encode the $json string using url_encode. You can pass this via the url.

Decode this URL, then decode the JSON object as an associative array.

might be worth a try


If you want to call a function of one controller from another controller then you can use redirect Helper.

For example:

class Logout extends CI_Controller { 

     function index() {     
        session_destroy();
        redirect('index.php/home/', 'refresh');
     } 

}

it will call another contoller.

0

精彩评论

暂无评论...
验证码 换一张
取 消