PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

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

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 the Lead Architect for Digg where he spends his time partitioning data, creating internal services, and ensuring the code frameworks are in working order.


Return to the PHP DevCenter.


Have a question about any of the articles in this series? Here's the place to ask.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 7 of 7.

  • there is any sample in php 4.x
    2007-06-29 12:54:26  satwizard [Reply | View]

    Hi...

    this is nice article.

    i need same for php 4.x

    bez my server support only php 4.x


    Thanks
  • How to load several modules in a time?
    2007-05-22 08:51:36  Einstijn [Reply | View]

    Hello,

    first of all, thanks to Joe Stump for this great series of articles. It really has been a great help for me!

    Now my question :)
    Has any body been able to load several modules at a time? I would like to show the latest newspost and a random picture (from my module photoalbum) in the sidebar of my website. But I doen't really now how to do that. I tried to place the following code in the controller (index.php).


    require_once('../modules/news/news.php');
    $instance = new news();
    $module = "news";
    $event = '__default';

    if (!PM_Module::isValid($instance)) {
    die("Requested module is not a valid framework module!");
    }

    $instance->moduleName = $module;

    if ($instance->authenticate()) {
    try {
    $result = $instance->$event();
    if (!PEAR::isError($result)) {
    // $presenter = PM_Presenter::factory($instance->presenter,$instance);

    /*
    if (!PEAR::isError($presenter)) {

    $presenter->display();
    } else {
    die($presenter->getMessage());
    }
    }
    } catch (Exception $error) {
    die($error->getMessage());
    }
    } else {
    die("You do not have access to the requested page!");
    }


    But this off course doesn't work. Is there anyone with a solution for this problem?

    thx in advance!
    • How to load several modules in a time?
      2008-07-06 10:27:04  joshsherman [Reply | View]

      Probably a bit late on my reply but in case you are still faced with the dilemma, have you thought about creating an additional module that would utilize objects based on the other modules? That would make the most sense as you're adding domain logic to your controller and that's not very MVC.
  • N-Tier Enterprise apps and MVC
    2006-04-07 02:12:18  bphiett [Reply | View]

    I'm working on an n-tier .NET app and also MVC web apps.
    Both look suprisingly similar to me, separation of presentation, data and logic.
    Am i right in thinking they are effectively the same concept or am I missing something?
  • Example.php
    2006-03-03 09:37:41  joestump98 [Reply | View]

    Example.php doesn't exist. You'll need to create your own Framework_Site class. It's on my TODO to add an *entire* example docroot to the package, but I haven't had time to do so yet.
    • Example.php
      2006-03-04 08:49:41  ikhnaton2 [Reply | View]

      Thanks for the reply. I'm trying actually but I think there're unmatching between the article and ur last coding structure. I'm trying to understand the structure separately out of the source code. I'm new to MVC and PHP5 OOP so it's confusing somehow. Wish u could update ur articles with the new code adding more thorough description and diagrams. Appreciate ur work and time to help.
  • Great article
    2006-03-02 21:48:38  ikhnaton2 [Reply | View]

    It's a great article Joe, I'm following your progress since the first article about MVC introduction. I have tried to make your Framework to work with me but I'm face some problems.
    Recently, when I tried Framework-0.1.1, I got the following error:
    Warning: Framework_Site::include_once(Framework/Site/Example.php) [function.include-once]: failed to open stream: No such file or directory in D:\Network\Apache Group\Apache2\htdocs\framework\Framework\Site.php on line 37

    This error is due to the line 15 in index.php:
    $result = Framework::start('Example');

    I tried to remove 'Example', exchange it with 'Default' but the same error. What should it be?


Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com