PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Oct 20, 2019 4:19 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri Sep 14, 2012 8:13 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
I am writing some code to login users and would like to use the MVC pattern when i do this. I'm rather new to the pattern and don't quite understand how it would work in terms of what the controller would be, what the model would be.

Here is my thinking -
1. Controller -> the login page
2. Model -> the class that logs in the user, checks for the username, etc.
3. View -> Not sure if this will have a view since there isn't really anything to display.

Any hints, advice & corrections will be appreciated.

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Fri Sep 14, 2012 2:26 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Design patterns, especially MVC, are not about fitting your code into their molds. It's about understanding the point behind them.
The point of MVC is to keep code that should reasonably be separate, separate. The database code doesn't have to care about how it's being used or where the data is being displayed, the controller code doesn't have to care about exactly how the models work or how the view will display some data, and the view code doesn't have to care about where the data it has came from.

With that said,
- Controller is what handles form submissions and logs the user in
- Model is the user class, and possibly a "form" class if you have one
- View is the page itself

But you say "since there isn't really anything to display"... Are you writing a login page or code to handle login form submissions happening anywhere?


Top
 Profile  
 
PostPosted: Fri Sep 14, 2012 3:00 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
the code will handle login form submissions; below are some functions i have mind (in no particular order) for the login class:

- check presence of the user in the database
- hash the password
- retrieve salt
- set session variables
- redirect user -> good user redirect
-> bad user redirect
- generate passkey

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Fri Sep 14, 2012 3:15 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Logging-in is an action, not an entity. It should not be its own class. Rather, it should be a method (or part of) on a reasonable class; I could see a Session::loginUser() method or the more typical User::login().

I enjoy over-architecting things so
Syntax: [ Download ] [ Hide ]
class User {

        public static function getByLogin($username, $password) {
                // check username+password in database
                // return a User if good, null/false/Exception if bad
        }

}

Syntax: [ Download ] [ Hide ]
abstract class Session {

        public static function loginUser($username, $password) {
                // get user from User::getByLogin()
                // set variables if successful
                // indicate status (eg, return bool or exception if bad)
        }

}

Syntax: [ Download ] [ Hide ]
class LoginForm {

        public function process() {
                // grab username and password from form
                // if Session::loginUser(username and password) works {
                //      redirect to good page
                // } else {
                //      redirect to bad page
                // }
        }

}

Syntax: [ Download ] [ Hide ]
$form = new LoginForm();
$form->process();


Top
 Profile  
 
PostPosted: Sat Sep 15, 2012 4:37 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Thank you for the feedback - definitely different from my original idea. I was thinking about having all of the functions i mentioned in a single class. Also the use of the static keyword; i wasn't aware of the fact the you can call a method defined as static without creating an instance of the class

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Sat Sep 15, 2012 4:50 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group