![]() |
class method from another class - 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: class method from another class (/showthread.php?tid=36359) Pages:
1
2
|
class method from another class - El Forum - 11-29-2010 [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! class method from another class - El Forum - 11-29-2010 [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). class method from another class - El Forum - 11-29-2010 [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 ![]() class method from another class - El Forum - 11-29-2010 [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. class method from another class - El Forum - 11-29-2010 [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! class method from another class - El Forum - 11-29-2010 [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 { models/user.php Code: class User extends Model { class method from another class - El Forum - 11-29-2010 [eluser]Avril[/eluser] [removed] class method from another class - El Forum - 11-29-2010 [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? class method from another class - El Forum - 11-29-2010 [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? or Quote:User: Hey, can I look at the information you have about me? class method from another class - El Forum - 11-29-2010 [eluser]Avril[/eluser] LOL, nice story for explaining it ![]() Thanks! I'll go ahead and try this all out, doesn't seem to be to hard. ![]() |