Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created April 10, 2011 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callumacrae/912297 to your computer and use it in GitHub Desktop.
Save callumacrae/912297 to your computer and use it in GitHub Desktop.
<?php
define('ROOT', __DIR__);
$path_info = explode('/', trim($_SERVER['PATH_INFO'], ' /'));
if (!isset($path_info[0]) || !isset($path_info[1]))
{
trigger_error('Path not specified', E_USER_ERROR);
}
$controller_path = ROOT . '/controllers/' . $path_info[0] . '.php';
if (!is_readable($controller_path))
{
trigger_error('Controller not found', E_USER_ERROR);
}
include($controller_path);
$controller = 'Framework\\Controllers\\' . $path_info[0];
$controller = new $controller;
if (!method_exists($controller, $path_info[1]))
{
trigger_error('Method does not exist', E_USER_ERROR);
}
if (isset($path_info[2]))
{
$controller->{$path_info[1]}($path_info[2]);
}
else
{
$controller->{$path_info[1]}();
}
CREATE TABLE users (
user_id int(10) NOT NULL AUTO_INCREMENT,
username varchar(20) NOT NULL,
email varchar(100) NOT NULL,
phone varchar(20) NOT NULL,
addr text NOT NULL,
PRIMARY KEY (user_id)
) ENGINE=MyISAM ;
INSERT INTO users (user_id, username, email, phone, addr) VALUES
(3, 'callumacrae', 'callum@donotwant.spam', '+447845722159', '10 Downing Street,\nLondon,\nSW1A 2AA'),
(4, 'tester', 'invalid@email.address', '078934515246', 'Bridge Street,\nWorcester,\nWorcestershire');
<?php
namespace Framework\Controllers;
//include the model
require_once(ROOT . '/models/user.php');
use \Framework\Models\UserModel as UserModel;
class User
{
public function __construct()
{
$this->user_model = new UserModel;
}
public function info($id)
{
$user = $this->user_model->get($id);
foreach($user as &$info)
{
$info = htmlspecialchars($info);
}
$user->addr = nl2br($user->addr);
//load the view
include(ROOT . '/views/user_info.php');
}
public function all()
{
$users = $this->user_model->all();
//load the view
include(ROOT . '/views/user_list.php');
}
}
<?php
namespace Framework\Models;
class UserModel
{
public function __construct()
{
// Database connection info
$host = 'localhost';
$port = 3306;
$database = 'test';
$username = 'root';
$password = '';
// Construct the DSN
$dsn = "mysql:host=$host;port=$port;dbname=$database";
// Create the connection
$this->db = new \PDO($dsn, $username, $password);
}
public function get($id)
{
$statement = $this->db->prepare('SELECT * FROM users WHERE user_id = ?');
$statement->execute(array($id));
return ($statement->rowCount()) ? $statement->fetchObject() : false;
}
public function all()
{
$statement = $this->db->query('SELECT * FROM users');
return ($statement->rowCount()) ? $statement->fetchAll(\PDO::FETCH_OBJ) : false;
}
}
<!DOCTYPE html>
<html>
<head>
<title>User info &bull; <?php echo $user->username; ?></title>
</head>
<body>
<h1><?php echo $user->username; ?></h1>
<p><strong>Username:</strong> <?php echo $user->username; ?></p>
<p><strong>User ID:</strong> <?php echo $user->user_id; ?></p>
<p><strong>Email:</strong> <a href="mailto:<?php echo $user->email; ?>"><?php echo $user->email; ?></a></p>
<p><strong>Phone:</strong> <?php echo $user->phone; ?></p>
<p><strong>Address:</strong><br /><?php echo $user->addr; ?></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>List of all users</title>
</head>
<body>
<h1>List of all users:</h1>
<ul>
<?php foreach($users as $user): ?>
<li><strong><a href="../info/<?php echo $user->user_id; ?>"><?php echo $user->username; ?></a></strong> (<a href="mailto:<?php echo $user->email; ?>">email them</a>)</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment