PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Implementing MVC in PHP: The Model
Pages: 1, 2, 3, 4

FR_Auth_User

Along with a session class and a user object, the code needs a proper authentication class to applications for users who are logged in. As a result, I created a new class called FR_Auth_User and put it into includes/Auth/User.php.



Because my login.php module class sets a session variable called userID to the user ID of the person logged in and that value is numerical, it becomes quite easy to validate that the user is logged in. The user ID of zero indicates an anonymous surfer, which means that if the user ID is greater than zero, the person is logged in.

<?php

  /**
  * FR_Auth_User
  *
  * @author Joe Stump <joe@joestump.net>
  * @copyright Joe Stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php 
  * @package Framework
  * @filesource
  */

  /**
  * FR_Auth_User
  *
  * If your module class requires that a user be logged in in order to access
  * it then extend it from this Auth class. 
  *
  * @author Joe Stump <joe@joestump.net>
  * @package Framework
  */
  abstract class FR_Auth_User extends FR_Auth
  {
      function __construct()
      {
          parent::__construct();
      }

      function authenticate()
      {
          return ($this->session->userID > 0);
      }

      function __destruct()
      {
          parent::__destruct();
      }
  }

?>

As you can see, the authenticate() method simply checks to make sure the session variable is greater than zero.

login.php

That's everything necessary to create a program: a simple application that validates a login form. The minimum necessary files for creating an application in any module are:

  • login.php, the Model. It houses all of the PHP code that validates the user, queries the database, and sets the appropriate session variables.
  • login.tpl, the Smarty template used by the default presenter. It's just a file with a simple HTML form.

For this specific module class, I have created the module users in the modules directory. Thus the module class file is modules/users/login.php and the template file is modules/users/tpl/login.tpl.

[code for modules/users/tpl/login.php]

You may be thinking, I read 8,000 words for this? Consider what the framework allows you to do. When my module loads and I finally start programming, I don't have to worry about creating sessions, connecting to databases, opening log files, or authenticating. For once, I don't have to worry that I missed an include file or forgot a snippet of common code.

This, in a nutshell, is the beauty of MVC programming. Because the controller and views have already been programmed and are in working order, you can focus on what you get paid to do: creating applications. I remember the pain of creating large sed and awk scripts to change database connections, config file locations, and so on. I remember having a header.php and a footer.php at the top and bottom of every single script.

I remember having hundreds of files that ran a function check_user() or something like that. What if I had to change that function name or integrate a different authentication method? If I added another check for a different authentication method, then that code/check would be run in every single file on every request. Now I just create a new FR_Auth_MyAuth and change the applications that need that specific authentication to extend that auth class.

Pages: 1, 2, 3, 4

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: