Welcome Guest, Not a member yet? Register   Sign In
Calling a model from a view?
#1

[eluser]eboxhead[/eluser]
Im trying to create a password encryption function that can be called when needed.

I can get it to work fine in a view, but once i put it in a model i just errors and Im not sure why.


Encryption
Code:
<?


class Encrypt extends Model() {

function Encrypt() {
    
   parent::Model;
}

function encryptpassword($random, $password) {
    $secure=sha1($random.$password);
    return $secure;

}

The View (quite vanilla)
Code:
<html>
    <head>

    </head>
    <body>
        <?php

        $random = "sddkldksdklds";
        $password = "password";

        $this->load->model('Encrypt');
        $this->Encrypt->passwordencrypt($random, $password);

        echo $secure;

        ?>
    </body>
</html>

Im sure its somethign simple, but what am i missing?
#2

[eluser]eboxhead[/eluser]
i see the syntax, didnt actually copy and paste . . . but im looking at this and thinking that maybe creating a library fro soemthign like this would be better than placing it in a model?
#3

[eluser]tomdelonge[/eluser]
Maybe your specific needs are different, but generally, your model could have the "encrypt password" function just fine. You'd use it in the controller. When you needed to echo something, you pass it to your view from your controller and echo it out.

Also, if your model is the only class that needs the function, there's no use in making a library for it. It just depends on where you need the function to be used from.
#4

[eluser]tomdelonge[/eluser]
Something's weird with the posting timestamps in this thread...

This is my third post in this thread. The one above this is my second. The one below it is my first. The times are weird.

Oh well, that should help with the context and all...
#5

[eluser]tomdelonge[/eluser]
In the model, the function is called "encryptpassword", in the view, you're calling "passwordencrypt". That function might not exist.

Hope that helps...
#6

[eluser]bretticus[/eluser]
Why bother putting that code in the view at all? The point of the view is to abstract as much business logic away from it as possible. Yes, you have to put a textual representation in the view, but I see no reason why you can't pass the computed value to the view as a single variable?


Yes, it's a good idea to abstract this to a model (by the way, a model has essentially the same accessibility as a controller in CI. In other words, controller and models have pretty much the same access to database resources, etc.) But, this is probably something that won't be called often and probably doesn't rely too much on the rest of the framework for functionality. Thus, it probably fits the library paradigm better.

Try something like:


Code:
<?php
// controller
function the_page()
        $random = "sddkldksdklds";
        $password = "password";

        $this->load->model('Encrypt');
        $data['secure'] = $this->Encrypt->passwordencrypt($random, $password);
        
}
?>

Code:
<html>
    <head>

    </head>
    <body>
        <?=$secure?>
    </body>
</html>




Theme © iAndrew 2016 - Forum software by © MyBB