PHP Developers Network
http://forums.devnetwork.net/

Using the MVC pattern for login code.
http://forums.devnetwork.net/viewtopic.php?f=19&t=136707
Page 1 of 1

Author:  social_experiment [ Fri Sep 14, 2012 8:13 am ]
Post subject:  Using the MVC pattern for login code.

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.

Author:  requinix [ Fri Sep 14, 2012 2:26 pm ]
Post subject:  Re: Using the MVC pattern for login code.

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?

Author:  social_experiment [ Fri Sep 14, 2012 3:00 pm ]
Post subject:  Re: Using the MVC pattern for login code.

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

Author:  requinix [ Fri Sep 14, 2012 3:15 pm ]
Post subject:  Re: Using the MVC pattern for login code.

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

Author:  social_experiment [ Sat Sep 15, 2012 4:37 am ]
Post subject:  Re: Using the MVC pattern for login code.

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

Author:  requinix [ Sat Sep 15, 2012 4:50 pm ]
Post subject:  Re: Using the MVC pattern for login code.


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/