Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> Implementing MVC in PHP: The Model [Bookmark it]
Implementing MVC in PHP: The Model

Implementing MVC in PHP: The Model

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.

Prev  [1] [2] [3] [4] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    Important Notice for Apach
    Custom-Compiling Apache an
    Simplify Your Life with Ap
    Industrial-Strength Webcas
    Monitoring Apache Page-Loa
    Implementing MVC in PHP: T
    Simplify PHP Development w
    Improve Your Build Process
    Implementing MVC in PHP: T
    Important Notice for PHP D
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial