CodeIgniter Forums
Creating my library, using its class, passing it values and retrieving them - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Creating my library, using its class, passing it values and retrieving them (/showthread.php?tid=53798)



Creating my library, using its class, passing it values and retrieving them - El Forum - 08-09-2012

[eluser]alvaroeesti[/eluser]
Hello,

so,

What I want to do is this:

I have a code snippet that hashes passwords. I want to put it in a class, I will call the class from the Controller and will retrieve the returned value of the operation at the class and forward it to the Model.

1. First, from the controller I pass a value to the class of the library


Code:
$params = array('pass' => 'pass');
$this->load->library('myblowfish', $params);


2. The class of the library should return 2 values. Don't know how to write that. Something like this ?

Code:
return array($salt, $hashed_password);


3. And then the Controller should pick these values and forward them to the Model. I know how to pass them to the Model, but not quite how to receive them from the Class whose values it called.




Creating my library, using its class, passing it values and retrieving them - El Forum - 08-09-2012

[eluser]InsiteFX[/eluser]
Models would expect an associated array.
Code:
// Library Class

$data = array(
    'salt  => $salt,
    'pass' => $hashed_password
);

Return $data;



Creating my library, using its class, passing it values and retrieving them - El Forum - 08-09-2012

[eluser]john_j[/eluser]
I haven't tried this but maybe you can put it under 'application/helpers' as a function (let us call it function hash_pass() { //do whatever here }). Then, in your controller first load the helper file and then you can just use hash_pass();


Creating my library, using its class, passing it values and retrieving them - El Forum - 08-09-2012

[eluser]alvaroeesti[/eluser]
@Insite FX

thanks, indeed that should be the return command from the Library

but I am calling the library from the controller and that is where I want to return the values so that i can pass them together with the rest (first name etc that were at the controller obtained from a User form)

so as you can see my idea is: from the controller I get the data a user fills in the Form. That is fine. However, before I pass the password to the Model, I want to send it to the library for hashing. So once I get the salt and the hashed password (both calculated in the library) I send them to the controller and from here, like I said, to the model

Basically what I have pending now is how to retrieve these return array from the Library at the Controller in such a way that I can have it suitable for forwarding it to the Model.


This is my calling code to the Library where I pass it the password then you wrote the return array from the Library. Alright, and where do I retrieve, or how, at the controller, this data array returned by the library ?

Code:
extract($_POST);
$params = array('pass' => 'pass');
$this->load->library('myblowfish', $params);

something like this, I suppose

Code:
$data = $this->load->library('myblowfish', $params);

And another question, is that the userguide says that you need to create a constructor in the class of the library if you are passing it arguments (from the controller). So I did, but, what do you place in the constructor?

Code:
public function __construct($pass)
  {
   // Do something with $params
  }


This below is not passing any value to the class in the Library
Code:
$result = $this->load->library('myblowfish', $pass);

And the beginning of the library is:

Code:
class Myblowfish {
  
  public function __construct()
  {
   print "la pass es $pass";
  }

     public function hashit($pass)
     {

I finally got it