Skip to content

Commit 023cd64

Browse files
author
nikksan
committed
second commit
1 parent 4edcd46 commit 023cd64

File tree

12 files changed

+222
-0
lines changed

12 files changed

+222
-0
lines changed

.htaccess

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RewriteEngine on
2+
RewriteCond %{REQUEST_FILENAME} !-d
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteRule . index.php [L]

config.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
define('DIR_CONTROLLER', dirname(__FILE__) . '/controller/');
3+
define('DIR_MODEL' , dirname(__FILE__) . '/model/');
4+
define('DIR_VIEW' , dirname(__FILE__) . '/view/');
5+
define('DIR_SYSTEM' , dirname(__FILE__) . '/system/');
6+
7+
8+
//Databse settings
9+
define('DB_HOSTNAME', 'localhost');
10+
define('DB_USERNAME', 'root');
11+
define('DB_PASSWORD', '');
12+
define('DB_DATABASE', 'test');
13+
define('DB_PORT', '3306');
14+
define('DB_PREFIX', 'rest_');

controller/Home.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
class Home extends Controller{
3+
public function index(){
4+
$data = array('key' => 'Test');
5+
6+
$this->load->view('index.html', $data);
7+
}
8+
}

index.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require_once "config.php";
3+
require_once "startup.php";
4+
5+
6+

route.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/* Place your routes here! */
3+
4+
Route::get('/rest/', 'Home@index');
5+
Route::dispatch();

startup.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
require_once DIR_SYSTEM . 'Route.php';
3+
require_once DIR_SYSTEM . 'Controller.php';
4+
require_once DIR_SYSTEM . 'Db.php';
5+
require_once DIR_SYSTEM . 'Session.php';
6+
require_once DIR_SYSTEM . 'Loader.php';
7+
8+
require_once "route.php";

system/Controller.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
class Controller{
3+
protected $db = null;
4+
protected $session = null;
5+
6+
public function __construct(){
7+
$this->db = new Db(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
8+
$this->session = new Session();
9+
$this->load = new Load();
10+
}
11+
}

system/Db.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
class Db{
3+
private $link;
4+
5+
public function __construct($servername, $username, $password , $database, $port = '3306'){
6+
if (!$this->link = mysql_connect($servername . ':' . $port, $username, $password)) {
7+
trigger_error('Error: Could not make a database link using ' . $username . '@' . $servername);
8+
exit();
9+
}
10+
11+
if (!mysql_select_db($database, $this->link)) {
12+
trigger_error('Error: Could not connect to database ' . $database);
13+
exit();
14+
}
15+
16+
mysql_query("SET NAMES 'utf8'", $this->link);
17+
mysql_query("SET CHARACTER SET utf8", $this->link);
18+
mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->link);
19+
mysql_query("SET SQL_MODE = ''", $this->link);
20+
}
21+
22+
public function query($sql) {
23+
if ($this->link) {
24+
$resource = mysql_query($sql, $this->link);
25+
26+
if ($resource) {
27+
if (is_resource($resource)) {
28+
$i = 0;
29+
30+
$data = array();
31+
32+
while ($result = mysql_fetch_assoc($resource)) {
33+
$data[$i] = $result;
34+
35+
$i++;
36+
}
37+
38+
mysql_free_result($resource);
39+
40+
$query = new \stdClass();
41+
$query->row = isset($data[0]) ? $data[0] : array();
42+
$query->rows = $data;
43+
$query->num_rows = $i;
44+
45+
unset($data);
46+
47+
return $query;
48+
} else {
49+
return true;
50+
}
51+
} else {
52+
$trace = debug_backtrace();
53+
54+
trigger_error('Error: ' . mysql_error($this->link) . '<br />Error No: ' . mysql_errno($this->link) . '<br /> Error in: <b>' . $trace[1]['file'] . '</b> line <b>' . $trace[1]['line'] . '</b><br />' . $sql);
55+
}
56+
}
57+
}
58+
59+
60+
61+
}

system/Loader.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
class Load{
3+
4+
/* View loader */
5+
public function view($template, $data, $return = false){
6+
if(!file_exists(DIR_VIEW . $template)){
7+
echo DIR_VIEW . $template . " doesnt exist!";
8+
exit(1);
9+
}else{
10+
extract($data);
11+
unset($data);
12+
13+
if($return){
14+
ob_start();
15+
require_once DIR_VIEW . $template;
16+
$output = ob_get_contents();
17+
ob_end_clean();
18+
return $output;
19+
}else{
20+
require_once DIR_VIEW . $template;
21+
}
22+
23+
}
24+
}
25+
}

system/Route.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
class Route{
3+
private static $GET = array();
4+
private static $POST = array();
5+
6+
public static function get($route, $action){
7+
self::$GET[$route] = $action;
8+
}
9+
10+
public static function post($route, $action){
11+
self::$POST[$route] = $action;
12+
}
13+
14+
public static function dispatch(){
15+
$action = isset( self::${$_SERVER['REQUEST_METHOD']}[$_SERVER['REQUEST_URI']] ) ? self::${$_SERVER['REQUEST_METHOD']}[$_SERVER['REQUEST_URI']] : false;
16+
if(!$action){
17+
echo "No action found this route!";
18+
exit(1);
19+
}else{
20+
$parts = explode('@', $action);
21+
$controller = $parts[0];
22+
$method = isset($parts[1]) ? $parts[1] : 'index';
23+
$file = DIR_CONTROLLER . $controller . '.php';
24+
if(!file_exists($file)){
25+
echo "File ( ".$file." ) does not exist!";
26+
exit(1);
27+
}else{
28+
require_once $file;
29+
if(!class_exists($controller)){
30+
echo "Class (".$controller.")does not exist!";
31+
exit(1);
32+
}else{
33+
$obj = new $controller();
34+
if(!method_exists($obj, $method)){
35+
echo "Method (".$method.") does not exist!";
36+
exit(1);
37+
}else{
38+
$obj->$method();
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}

system/Session.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
class Session{
3+
private $data = array();
4+
5+
public function __construct(){
6+
if (session_status() == PHP_SESSION_NONE) {
7+
try{
8+
session_start();
9+
}catch(Exception $e){
10+
throw $e->getMessage();
11+
}
12+
}
13+
14+
$this->data = $_SESSION;
15+
}
16+
17+
18+
public function set($key, $value){
19+
$this->data[$key] = $value;
20+
}
21+
22+
public function get($key){
23+
return isset($this->data[$key]) ? $this->data[$key] : null;
24+
}
25+
26+
public function remove($key){
27+
unset($this->data[$key]);
28+
}
29+
30+
31+
public function __destruct(){
32+
$_SESSION = $this->data;
33+
}
34+
35+
}

view/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php var_dump($key);?>

0 commit comments

Comments
 (0)