0

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)){ 
   // 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?

3 Answers 3

2

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

That should work.

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

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible if $data is array?
urlencode (no underscore) expects a string, so no an array won't work. see my answer for a possible solution
0
<?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

2 Comments

Agreed: For an array JSON and urlencoding is definitely the best choice.
ok.. i will try it.. thanks.. is there any limit of lenght of http url?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.