TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-12-2008, 12:19 AM   #1 (permalink)
The Visitor
 
Join Date: Jul 2008
Location: Florida
Posts: 3
Thanks: 1
cptnbunghole is on a distinguished road
Default OOP - Question[s]

Introduction: [Not Important]
Well I am pretty new to PHP programming and have been doing it for about 2 months on and off but I finally broke down and had to ask a few questions that have been stumping me with my code...

Question:
First off I am playing with OOP and MVC pattern and I am just basically experimenting with it and trying to learn how things work. Now Ive run into a question, my problem is I have a log in form here...

HTML Code:
<form name="login" method="post" action="">
<table width="200" height="100" border="1" bordercolor="#000066" align="center">
<tr><td bordercolor="#000066" colspan="2" align="center">Login</td></tr>
<tr><td bordercolor="#000066">Username:</td><td bordercolor="#000066"><input type="text" name="user" width="300"></td></tr>
<tr><td bordercolor="#000066">Password:</td><td bordercolor="#000066"><input type="password" name="pass" width="300"></td></tr>
<tr><td bordercolor="#000066" colspan="2" align="right"><input type="submit" name="login" value="Login"></td></tr>
<tr><td bordercolor="#000066" colspan="2" align="right"><a href="login.php?action=logout">Logout</a></td></tr></table>
</form>
It just a basic form but I am not sure where to send the data?

HTML Code:
<form name="login" method="post" action="">
I have my controller which handles the logic...

PHP Code:
<?php

require_once dirname(__FILE__) . "/action.php";

class 
User_Controller extends ActionController {



  public function 
doLogin() {
    
  }
  
  public function 
doRegister() {
    
  }


}


?>
(I stripped down the controller just so you have a basic idea of what im asking here.)

So again the question is how do I get the form to post to the controller(or wherever it should go) so I can then execute the rest of the log in process.

If my question Isn't clear I will try and explain more clearly or if you need more code examples or details just let me know. Thanks so much
Send a message via MSN to cptnbunghole
cptnbunghole is offline  
Reply With Quote
Old 07-12-2008, 12:51 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

You can send it anywhere you want, basically.

Two options leap to mind;

Send it to itself. Include some kind of procedural code in the head of your login page that looks something like:

PHP Code:
<?php

if (isset($_POST['login']))
{
    
$pCtrl = new User_Controller;
    
$pCtrl->doLogin();
}

?>
<!-- insert form here -->
Or you can send it to an intermediary controller. I use one in a system I wrote that I send all forms to, and it determines which sub-system it needs to load, and the actions to take from there. It then sends you back to the form if there are errors, or on to whatever page should follow.

OOP is like driving in the city, there's about 30 ways to get anywhere. You could additionally contain the view inside the class, and just instantiate a new object on page load which would then determine if it needs to process the form, display the form, or redirect based on criteria you put into it's constructor.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-12-2008, 01:16 AM   #3 (permalink)
The Visitor
 
Join Date: Jul 2008
Location: Florida
Posts: 3
Thanks: 1
cptnbunghole is on a distinguished road
Default

Thanks a lot for the help and as I said I am new to this so correct me where I am wrong or let me know if I am missing the point.

I understand the first bit of code.

PHP Code:
<?php

if (isset($_POST['login']))
{
    
$pCtrl = new User_Controller;
    
$pCtrl->doLogin();
}

?>
<!-- insert form here -->
I used that same code in log in scripts before I learned about OOP and the MVC style. So if I where to include that code in the top of the log in form wouldn't that defeat the whole purpose of MVC, separating logic from view?

The second method "intermediary controller" sounds like the MVC style. How would you go about doing that? Cause I tried sending my form data to the user_controller.php

HTML Code:
<form name="login" method="post" action="<?php echo dirname(__FILE__) . "/user_controller.php"; ?>">
But all I got was an error from the browser(i think) saying it did not understand how to perform the task.
Send a message via MSN to cptnbunghole
cptnbunghole is offline  
Reply With Quote
Old 07-12-2008, 03:44 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Well, one way would be to transfer the logic to your user_controller.php file, something like this:

PHP Code:
 <?php 

require_once dirname(__FILE__) . "/action.php"

$pController = new User_Controller;
$pController->doLogin();

class 
User_Controller extends ActionController 



  public function 
doLogin() { 
     if (isset(
$_POST['login']))
     {
         
// do your stuff
     
}
     else
     {
         
// set an error, redirect, whatever you want here
     
}
  } 
   
  public function 
doRegister() { 
     
  } 





?>
You can also play around with using __construct() to determine the course of action for the object when it's instantiated.

If you're not yet familiar with them, you can read up on them at the PHP site.
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
cptnbunghole (07-12-2008)
Old 07-12-2008, 04:12 PM   #5 (permalink)
The Visitor
 
Join Date: Jul 2008
Location: Florida
Posts: 3
Thanks: 1
cptnbunghole is on a distinguished road
Default

Well thanks so much delayedinsanity for your help, I figured out why it was not working in the first place, I had the log in form pointing to the wrong file... so when I changed it to the correct one it executed perfectly. Once again thanks.

Also I do have another question about my front controller. It works perfectly when I point it to a file I have made, and it also works when a page cannot be found but I cant figure out how to make it have a default page to display for my index.

Example:
If I go to a page that exists like

http://localhost/rome/index.php?page=user&action=login

Then it loads it up perfectly.

And when I go to a page that doesn't exist like

http://localhost/rome/index.php?page=free&action=money

it displays the 404 error page like it should...

So the question is when I want it to point to the main index file

http://localhost/rome/index.php

How do I get it to load up a default home page when someone goes to the index.php? Because all I get from it is my 404 error page.

index.php
PHP Code:
<?php

define
("CONTROLLER_DIR"dirname(__FILE__) . "/app/controller");
define("MODEL_DIR"dirname(__FILE__) . "/app/model");
define("PAGE_DIR"dirname(__FILE__) . "/app/view/pages");
define("WEBROOT_DIR"dirname(__FILE__) . "/app/webroot");

require_once 
dirname(__FILE__) . '/app/controller/front.php';
FrontController::createInstance()->dispatch();

?>
front.php
PHP Code:
<?php

require_once dirname(__FILE__) . "/controller.php";
require_once 
dirname(__FILE__) . "/action.php";
require_once 
dirname(__FILE__) . "/template.php";

class 
FrontController extends Controller {
  public static function 
createInstance() {
    if (!
defined("PAGE_DIR")) {
      exit(
"Critical error: Cannot proceed without PAGE_DIR.");
    }
    if (!
defined("CONTROLLER_DIR")) {
      exit(
"Critical error: Cannot proceed without CONTROLLER_DIR.");
    }
    if (!
defined("MODEL_DIR")) {
      exit(
"Critical error: Cannot proceed without MODEL_DIR.");
    }
    if (!
defined("WEBROOT_DIR")) {
      exit(
"Critical error: Cannot proceed without WEBROOT_DIR.");
    }
    
$instance = new self();
    return 
$instance;
  }
  
public function 
dispatch() {
    
$page1 = !empty($_GET["page"]) ? $_GET["page"] : "home";
    
$action1 = !empty($_GET["action"]) ? $_GET["action"] : "index";
    
$page preg_replace("/[^a-zA-Z0-9_]+/"""$page1);
    
$action preg_replace("/[^a-zA-Z0-9_]+/"""$action1);
    
    
$this->forward($page$action);
  }
  
}
?>
action.php
PHP Code:
<?php

require_once dirname(__FILE__) . "/controller.php";

abstract class 
ActionController extends Controller {
  protected 
$name;
  protected 
$viewData = array();

  public function 
setName($name) {
    
$this->name $name;
  }
  public function 
getName() {
    return 
$this->name;
  }
  public function 
setVar($key$value) {
    
$this->viewData[$key] = $value;
  }
  public function 
getVar($key) {
    if (
array_key_exists($key$this->viewData)) {
      return 
$this->viewData[$key];
    }
  }
  public function 
__set($key$value)  {
    
$this->setVar($key$value);
  }
  public function 
__get($key) {
    return 
$this->getVar($key);
  }
  public function 
dispatchAction($action) {
    
$actionMethod "do" ucfirst($action);
    if (!
method_exists($this$actionMethod)) {
    
//exit("Page not found");
    
$tpl = & new TemplateController();
    
    
$tpl->displayTemplate();
    
    include 
PAGE_DIR "/error/404.tpl";
    exit;
    }
    
$this->$actionMethod();
    
$this->displayView($action);
  }
  public function 
displayView($action) {
    if (!
is_file(PAGE_DIR "/" $this->getName() . "/" $action ".tpl")) {
      
//exit("Page not found");
    
    
$tpl = & new TemplateController();
    
    
$tpl->displayTemplate();
    
    include 
PAGE_DIR "/error/404.tpl";
    exit;
    }
    
//Create variables for the template
    
foreach ($this->viewData as $key => $value) {
      $
$key $value;
    }
    
$tpl = & new TemplateController();
    
    
$tpl->displayTemplate();
    
    include 
PAGE_DIR "/" $this->getName() . "/" $action ".tpl";
    
    exit(
0);
  }
}
?>
controller.php
PHP Code:
<?php

require_once 'template.php';

abstract class 
Controller extends TemplateController {
  public function 
getRequest() {
  }
  public function 
getResponse() {
  }
  public function 
getSession() {
    
//I tend to prefer $this->getRequest()->getSession(), but whatever!
  
}
  
  public function 
forward($page$action) {
    
//e.g. HomeActions
    
$class ucfirst($page) . "_controller";
    
//e.g. pages/home/HomeActions.php
    //$file = CONTROLLER_DIR . "/" . $page . "/" . $class . ".php";
    
$file CONTROLLER_DIR "/" $class ".php";
    if (!
is_file($file)) {
    
//exit("Page not found");
    
$tpl = & new TemplateController();
    
    
$tpl->displayTemplate();
    
    include 
PAGE_DIR "/error/404.tpl";
    exit;
    }
    require_once 
$file;
    
$controller = new $class();
    
$controller->setName($page);
    
$controller->dispatchAction($action);
    exit(
0);
  }
  
}
?>
Send a message via MSN to cptnbunghole
cptnbunghole is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design