Using the MVC pattern for login code.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Using the MVC pattern for login code.

Post by social_experiment »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using the MVC pattern for login code.

Post by requinix »

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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Using the MVC pattern for login code.

Post by social_experiment »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using the MVC pattern for login code.

Post by requinix »

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

Code: Select all

class User {

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

}

Code: Select all

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)
	}

}

Code: Select all

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
		// }
	}

}

Code: Select all

$form = new LoginForm();
$form->process();
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Using the MVC pattern for login code.

Post by social_experiment »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using the MVC pattern for login code.

Post by requinix »

social_experiment wrote: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
That's the whole point of it :o Static methods let you keep functionality related to a specific thing (eg, sessions or users) in its class even when you don't need an instance of that thing (eg, a session or user) to do it. Like a "utility" class*.

Code: Select all

class Math {

	public static function add($a, $b) {
		return $a + $b;
	}

}
A trivial example to be sure, but it demonstrates the point: you don't actually need an instance of the Math class just for the addition.

They're also used commonly for factory-type methods: rather than construct an object and set values in your own code, you call a factory method to do that work for you. It means the logic for setting up the object in that way gets to remain in one place, and as a bonus in the same place where the rest of the object's code is.

Code: Select all

class Number {

	protected $num;

	public function __construct($num) {
		$this->num = $num;
	}

	public function add($num) {
		$this->num += $num;
	}

	public static function fromAddition($a, $b) {
		$object = new self($a);
		$object->add($b);
		return $object;
	}

}

Code: Select all

$a = new Number(5);
$a->add(10);

$b = Number::fromAddition(5, 10);
(Also a trivial example.)


* Avoid utility classes in PHP. If you need a function to do something, use a normal function.
Post Reply