• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating my library, using its class, passing it values and retrieving them



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

$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 ?

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.

Models would expect an associated array.
// Library Class

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

Return $data;

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();

@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 ?

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

something like this, I suppose

$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?

public function __construct($pass)
   // Do something with $params

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

And the beginning of the library is:

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

     public function hashit($pass)

I finally got it

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.