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

logout.php

Create a user account in fr_users for yourself, and go to the login module and log in. If you have the mod_rewrite rules turned on, you can simply go to http://example.com/users/login. If you don't, then go to http://example.com/index.php?module=users&class=login. Once you have logged in, you should be redirected to the home page, which should now recognize that you are logged in.



Before you click on the Submit link, right-click on it and copy the link to your clipboard. I'll explain why in a moment. OK, now go ahead and click on the link labeled Submit, which will log you out. (The links assume you have the rewrite rules turned on.)

The logout module should destroy your session and then send you back to the home page, which should no longer show that you are logged in. Now copy and paste the logout URL, and try to go to the module again. You should see the error "You do not have access to the requested page!"

Because the class logout extends FR_Auth_User, it requires that the user be logged in. The function FR_Auth_User::authenticate() checks to make sure $this->session->userID is greater than zero. When the controller runs logout::authenticate() (inherited from FR_Auth_User) and a person is not logged in, the function returns false, which tells the controller to bomb out.

whoami.php

As a small example of how you can combine the various pieces to the MVC puzzle, I created a simple module class called whoami.php. The script simply displays who the user is. However, it checks for a GET argument called output. If output is rest, the module switches to the FR_Presenter_rest view, which outputs the module's $data variable in valid XML using PEAR's XML_Serializer class.

<?php



  /**

  * whoami

  *

  * @author Joe Stump <joe@joestump.net>

  * @copyright Joe Stump <joe@joestump.net>

  * @license http://www.opensource.org/licenses/gpl-license.php

  * @package Modules

  * @filesource

  */



  /**

  * whoami

  *

  * @author Joe Stump <joe@joestump.net>

  * @package Modules

  */

  class whoami extends FR_Auth_User

  {

      public function __construct()

      {

          parent::__construct();

      }



      public function __default()

      {

          $this->set('user',$this->user);

          if ($_GET['output'] == 'rest') {

              $this->presenter = 'rest';

          }         

      }



      public function __destruct()

      {

          parent::__destruct();

      }

  }



?>

Conclusion

Some people think that MVC frameworks are heavy. That may be; however, my own MVC application run web sites that regularly receive more than 10 million page views per month. If the load gets too heavy, I simply turn on caching in my presentation layer.

The gains I have experienced in my development cycles vastly outweigh the perceived problem of heaviness. If programmed and documented properly, MVC frameworks reduce the time you spend debugging your code; they increase your productivity; and they greatly reduce the barrier to entry for new and junior programmers.

Download the Source

I am working on a version of this framework that I am releasing under the BSD license. I plan on using PEAR to package it up nicely, though I doubt it will be an official PEAR package. Keep your eye on MVC Framework for PHP5 for updates. You can download and install it via PEAR right now if you wish.

Joe Stump is currently the technology manager for eNotes.com.


Return to the PHP DevCenter.

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

[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