|
|||||||
New to SitePoint Forums? Register here for free!
|
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Jan 2007
Posts: 10
|
My attempt to MVC
Im trying to get a hold of the whole MVC concept.
This is my coding so far. Am i at the right track? What do i need to change and what is good with my coding? Thanx in advance, Crille Structure of the folders and files Code:
.models ..guestbook ...guestbookModel.php .controllers ..guestbook ...guestbookController.php .views ..guestbook ...view.tpl ...post.tpl index.php Code:
<?php
include ('includes/config.php');
if (isset($_GET['module'])) {
$module = $_GET['module'];
if (isset($_GET['action'])) $action = $_GET['action'];
else $action = 'view';
}
else {
$module = 'default';
$action = 'view';
}
$model = 'models/' . $module . '/' . $module . 'Model.php';
$controller = 'controllers/' . $module . '/' . $module . 'Controller.php';
$template = 'views/' . $module . '/' . $action . '.tpl';
if (!file_exists($model)) {
header ("Location: index.php");
}
if (!file_exists($controller)){
header ("Location: index.php");
}
if (!file_exists($template)){
header ("Location: index.php");
}
$modelClass = $module . 'Model';
$controllerClass = $module . 'Controller';
include $model;
include $controller;
$controller = new $controllerClass();
$controller->setData("site", "model", $modelClass);
$controller->setData("site", "template", $template);
$controller->setData("site", "action", $action);
$controller->prepare();
$controller->render();
?>
Code:
<?php
class guestbookController {
private $registery = array();
public function __construct() {
}
/*
* ::Register set
* Sets a registery key
*/
public function setData($type, $key, $value) {
$this->registery[$type][$key] = $value;
}
public function getData($type, $key) {
return $this->registery[$type][$key];
}
public function printErrors() {
if (!empty($this->registery["error"])) {
$error = "";
foreach ($this->registery["error"] as $key => $value) {
$error .= $value;
}
return $error;
}
}
/*
* ::Validate form
* Checks if a form is valid
*/
public function formCheck($form) {
foreach ($form as $key => $value) {
if (!empty($value)) {
switch ($key) {
case 'email':
$validate = new validate;
if (!$validate->email($value)) $this->setData("error", "$value", "<p>$value är en felaktig E-Post!</p>");
break;
case 'url':
$validate = new validate;
if (!$validate->url($value)) $this->setData("error", "$key", "<p>$value är en felaktig URL</p>");
}
}
else $this->setData("error", "$key", "<p>$key måste fyllas i!</p>");
}
}
/*
* ::Prepare
* Prepares actions for the page
*/
public function prepare() {
switch ($this->getData("site", "action")) {
case 'post':
if ($_POST) {
$this->formCheck($_POST);
if (empty($this->registery["error"])) {
$headline = htmlspecialchars(str_replace("|", "", $_POST['headline']));
$name = htmlspecialchars(str_replace("|", "", $_POST['name']));
$email = $_POST['email'];
$text = htmlspecialchars(str_replace("|", "", $_POST['textarea']));
$time = date("Y:m:d");
$instance = new guestbookModel;
$instance->write($headline, $name, $email, $text, $time);
}
}
break;
case 'view':
$instance = new guestbookModel;
$this->setData("guestbook", "posts", $instance->read());
$guestbook = $this->getData("guestbook", "posts");
$lines = array_reverse(explode("\r\n", $guestbook));
foreach ($lines as $line) {
if (!empty($line)) $posts[] = explode("|", $line);
}
$this->setData("guestbook", "posts", $posts);
break;
}
}
/*
* ::Render
* Renders the page
*/
public function render() {
include $this->getData("site", "template");
}
}
?>
Code:
<?php
class guestbookModel {
private $file = 'guestbook.txt';
/*
* ::Write
* Writes to the guestbook file
*/
public function write($headline, $name, $email, $text, $time) {
$content = file_get_contents($this->file);
$content .= "$headline|$name|$email|$text|$time" . "\r\n";
file_put_contents($this->file, $content);
}
/*
* ::Read
* Reads from the guestbook file
*/
public function read() {
return file_get_contents($this->file);
}
}
?>
Code:
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<?php
foreach ($this->getData("guestbook", "posts") as $value) {
?>
<p><strong>Rubrik</strong></p>
<p><?php echo $value[0] ?></p>
<p><strong>Av</strong></p>
<p><?php echo $value[1] ?></p>
<p><strong>E-Post</strong></p>
<p><?php echo $value[2] ?></p>
<p><strong>Meddelande</strong></p>
<p><?php echo $value[3] ?></p>
<p><strong>Skrivet</strong></p>
<p><?php echo $value[4] ?></p>
<hr />
<?php
}
?>
<p><a href="index.php?module=guestbook">Visa gästboken</a></p>
<p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a>
Code:
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" /> <?php echo $this->printErrors(); ?> <form method="post"> <p><strong>Rubrik</strong></p> <p><input name="headline" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['headline'] ?>" size="30" /></p> <p><strong>Namn</strong></p> <p><input name="name" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['name'] ?>" size="30" /></p> <p><strong>E-Post</strong></p> <p><input name="email" type="text" value="<?php if (isset($_POST['submit'])) echo $_POST['email'] ?>" size="30" /></p> <p><strong>Text</strong></p> <p><textarea name="textarea" cols="50" rows="5"><?php if (isset($_POST['submit'])) echo $_POST['textarea'] ?></textarea></p> <p><input name="submit" type="submit" value="Skicka" /></p> </form> <hr /> <p><a href="index.php?module=guestbook">Visa gästboken</a></p> <p><a href="index.php?module=guestbook&action=post">Skriv i göstboken</a></p> |
|
|
|
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 02:45.








Threaded Mode