Welcome Guest, Not a member yet? Register   Sign In
class method from another class
#1

[eluser]Avril[/eluser]
Hello,

I have made a "logged_in" method in a class called "Site", this object is a controller so it extends the Controller class.

Now I have another controller called "Expenses" where I want to call the logged_in method from the Site controller, how can I call this method?

Thanks!
#2

[eluser]Michael Wales[/eluser]
You really don't want to - you should look at refactoring your application a bit.

For instance, a controller named site - this leads me to believe your entire site is contained within this controller, I question why you need any others - this is your "site".

A method logged_in within the class Site, what is your site logging in to? Why would an expense controller need to know if your site was logged in to something. How does a site login to another site, I thought users were going to be logging in...

These are all very silly questions, but from an application design standpoint - it's what your application says it does.

Your expenses controller is a good start - it's a descriptive name, it tells us exactly what is going on here. This is everything an expense can do.

Your logged_in method, to me, sounds like user authentication. This has nothing to do with your site - we're talking about users here. I would make an authentication library (which would be accessible by other controllers), you'll then need an accounts/users controller, to respond to the user's request (user wants to login, they hit accounts->login() method, on successful form validation the authentication library is called to confirm username/password).
#3

[eluser]Avril[/eluser]
I know that the naming of the "logged_in" isn't really in the correct location, but I'm new to CI so for the moment I don't know how to access a method from anther controller, that's why I've put it in the site controller for the time being, this way I could check the authentication, when I know how to access a method from another controller I'm going to move the method to the correct controllers.

That's why I've posted my first post to Wink
#4

[eluser]Michael Wales[/eluser]
My point is that it doesn't belong in a Controller. One Controller should never have a need to call another controller (unless you are dabbling in HMVC and even then, the terminology/concept isn't the same).

Hit the User Guide, research libraries, refactor your authentication mechanism.
#5

[eluser]Avril[/eluser]
euh ok, didn't know that it shouldn't be in a controller, I've checked a tutorial about this, and he did put it in a controller.

Question, is it a better security if I put it in a library rather then in a controller?
This small project is for local use, but I'm in a learning process and would like to have it as a real online application, this way it's a good exercise.

Thanks!
#6

[eluser]Michael Wales[/eluser]
No additional security, just an application design perspective (and how MVC is supposed to work).

M: Data (your application getting stuff from somewhere)
V: Views (what the user sees)
C: Actions (what can the user do)

Let's break down what you are trying to accomplish:

Task: My application needs to know if the user is currently logged in or not.

1. Is this an action the user invokes? Nope - not a controller.
2. Is this something the user can see? Nope - not a view.
3. Is my application going to be getting data from somewhere? Yeah, but it won't be apparent to the user.

So, we know logged_in is not a controller or a view, but we are going to need to access some data. The only choice remaining is a Library. This is awesome - since we know our other controllers (actions the user can take) are going to need to see if the user is logged in as well. Luckily for us, libraries are easily called by anything!

This is all very quickly slapped together but should get you in the right direction - read the user guide, don't copy-paste this in, if it doesn't work try and figure it out, ask questions when stuck.

libraries/authentication.php
Code:
class Authentication {

  var $CI;

  function __construct() {
    $this->CI &= get_instance();
  }

  function logged_in() {
    // If a user is logged in, they will have a session with:
    // id (the user's unique ID from database)
    // key (for the purpose of this example, this === salt from database table)
    $id = $this->session->userdata('id');
    $key = $this->session->userdata('key');
    $this->CI->load->model('user');

    $user = $this->CI->user->get(array('id' => $id, 'salt' => $key));
    if ($user !== NULL) return TRUE;
    return FALSE;
  }
}


models/user.php
Code:
class User extends Model {

  function get($params) {
    $query = $this->db->get_where('users', $params, 1, 0);
    if ($query->num_rows() > 0) {
      return $query->row();
    }
    return NULL;
  }

}
#7

[eluser]Avril[/eluser]
[removed]
#8

[eluser]Avril[/eluser]
Ok, seems not to be complicated.

Question, so I can create some session userdata within the library, but what I want that a view, for example the user account where he can see his account details like his account number, his current used email address etc etc ..

This view is only accessible if he’s logged in, so I could (correct me if I’m wrong) use a controller for this were I call the authentication and if he’s not logged in I redirect (or use an error message) the user/visitor?
#9

[eluser]Michael Wales[/eluser]
Quote:Question, so I can create some session userdata within the library, but what I want that a view, for example the user account where he can see his account details like his account number, his current used email address etc etc ..

This is the beauty of MVC - separation. Account number, email address, all of this is data. The view has absolutely no impact on this data, there is no correlation, it doesn't matter, no one cares, don't mention the two in the same sentence, don't think of the two at the same time. You could pump out email addresses to HTML views, RSS views, JSON views, an LED controller plugged into Frankenstein's ass making him fart in morse code - doesn't matter. Data has nothing to do with views. Now, I don't want you using the two of them in the same paragraph every again - stop it.

Quote:This view is only accessible if he’s logged in, so I could (correct me if I’m wrong) use a controller for this were I call the authentication and if he’s not logged in I redirect (or use an error message) the user/visitor?

Exactly! That's the point - your controller handles the request from the user. The user shouldn't have to decide if they are logged in or not, that's your applications problem.

Quote:User: Hey, can I look at the information you have about me?
Controller: I hear you - let me go check with my main man Authentication, see if you've identified yourself.
Authentication: Straight up fo' reals home-dog - he's mos-def user, ya heard?
Controller: Okay... wierdo. Hey User, yeah I'm gonna let you check this out. Let me just go get your file from that hot-chick down in Models, man I love seeing her. BRB
Model: Oh hey baby - when we gonna go out again? I had such a good time last week. Oh, strictly business huh? This better not be one of your girlfriends! Fine, here's that whore's info - you better delete my number from your phone and don't message me on Facebook either!
Controller: WTF? Women... I swear, crazy bitches. Alright user, I got your info right here. It's a damn mess though, Models just threw it at me from across the room. Go to the next window down, I'll hand this all off to View and he'll get it put back together so you can read it.
View: Man, this is bullshit... I can not wait to quit this job.

or

Quote:User: Hey, can I look at the information you have about me?
Controller: I hear you - let me go check with my main man Authentication, see if you've identified yourself.
Authentication: Hell's nah that ain't user - I'd know user any day of the motherfuckin' week and you ain't no user.
Controller: WTF "user"? You trying to play games with me son, huh? GTFO!
#10

[eluser]Avril[/eluser]
LOL, nice story for explaining it Big Grin

Thanks! I'll go ahead and try this all out, doesn't seem to be to hard. Smile




Theme © iAndrew 2016 - Forum software by © MyBB