Nieuw lid |
|
Hallo,
Ik heb een website waar ik een model view controller gebruik.
Nu is mijn index.php zo:
<?php
session_start();
require 'includes/startup.php';
# Connect to DB
try{
$db = new PDO('mysql:host=localhost;dbname=stenden', 'admin', 'admin');
$registry->set ('db', $db);
}
catch (PDOException $e) {
echo "Error: Couldn't open the DB. " . $e->getMessage();
}
# Load template object
$template = new Template($registry);
$registry->set ('template', $template);
# Load router
$router = new Router($registry);
$registry->set ('router', $router);
$router->setPath ('controllers');
# Load functions
require 'includes/functions.php';
# Set language
if(isset($_GET['lang']))
$_SESSION['lang'] = $_GET['lang'];
elseif(!isset($_SESSION['lang']))
$_SESSION['lang'] = "dutch";
if($_SESSION['lang']=="dutch")
{
$langMenu = "menunl";
$langNews = "newsnl";
$langEvent = "eventnl";
$registry['template']->set("newsHeader","Nieuws: ");
}
elseif($_SESSION['lang']=="english")
{
$langMenu = "menuen";
$langNews = "newsen";
$langEvent = "eventen";
$registry['template']->set("newsHeader","News: ");
}
elseif($_SESSION['lang']=="german")
{
$langMenu = "menudu";
$langNews = "newsdu";
$langEvent = "eventdu";
$registry['template']->set("newsHeader","Nachricht: ");
}
if(isset($_GET['tid']))
{
$_SESSION['menu_id'] = $_GET['tid'];
}
# Set menu
$sql = "SELECT menu.id menuid, menu.ordermenu menuorder, menu.link menulink, menu.ref menuref, menu2.name menunaam FROM menu JOIN ".$langMenu." as menu2 ON menu.id = menu2.menu_id WHERE menu.publishDate <= DATE(CURRENT_TIMESTAMP) ORDER BY menuref, menuorder";
$statement = $registry['db']->prepare($sql);
$statement->execute();
$rows = $statement->fetchAll();
$registry->set("menus",$rows);
$registry['template']->set("menus", $rows);
# Set admin menu
if(isset($_COOKIE['drentheTechniekLogin']))
{
$adminMenu = "";
$user = explode(":", $_COOKIE['drentheTechniekLogin']);
$result = runQuery("SELECT username, password, type, rights FROM user WHERE username='".$user[0]."' AND password='" .$user[1]. "'",$registry['db']);
if(isset($result[0]))
{
if($result[0]['type']=="1")
{
$adminMenu .= '<li>Beheer
<ul>
<li><a href="index.php?route=admin/gegevens">Userdata</a></li>
<li><a href="index.php?route=admin/userManagement">Usermanagement</a></li>
<li><a href="index.php?route=admin/addUser">Add user</a></li>
<li><a href="index.php?route=content/addContent">Add content</a></li>
<li><a href="index.php?route=content/editContentOverzicht">Contentoverview</a></li>
<li><a href="index.php?route=news/addNews">Add news</a></li>
<li><a href="index.php?route=events/addEvent">Add event</a></li>
<li><a href="index.php?route=admin/metaData">Metadata</a></li>
<li><a href="index.php?route=admin/logout">Logout</a></li>
</ul></li>';
}
elseif($result[0]['type']=="0")
{
$rights = explode(":", $result[0]['rights']);
$adminMenu .= '<li>Beheer
<ul>
<li><a href="index.php?route=admin/gegevens">Gegevens</a></li>';
if($rights[0])
$adminMenu .= '<li><a href="index.php?route=content/addContent">Add content</a></li>';
$adminMenu .= '<li><a href="index.php?route=content/editContentOverzicht">Contentoverview</a></li>';
if($rights[1])
$adminMenu .= '<li><a href="index.php?route=news/addNews">Add news</a></li>';
if($rights[2])
$adminMenu .= '<li><a href="index.php?route=events/addEvent">Add event</a></li>';
$adminMenu .= '<li><a href="index.php?route=admin/logout">Logout</a></li>';
$adminMenu .= '</ul></li>';
}
$registry['template']->set('adminMenu',$adminMenu);
}
}
# set Headers
$registry['template']->set("nieuwsteNews",runQuery("SELECT id, title, content FROM ".$langNews." ORDER BY id DESC LIMIT 1",$registry['db']));
$registry['template']->set("nieuwsteEvent",runQuery("SELECT id, startDate, title FROM ". $langEvent ." WHERE enddate >= DATE(NOW()) ORDER BY startDate, startTime LIMIT 1",$registry['db']));
$router->delegate();
$db = NULL;
?>
<?php require 'includes/startup.php'; # Connect to DB try{ $db = new PDO('mysql:host=localhost;dbname=stenden', 'admin', 'admin'); $registry->set ('db', $db); } catch (PDOException $e) { echo "Error: Couldn't open the DB. " . $e->getMessage(); } # Load template object $template = new Template($registry); $registry->set ('template', $template); # Load router $router = new Router($registry); $registry->set ('router', $router); $router->setPath ('controllers'); # Load functions require 'includes/functions.php'; # Set language $_SESSION['lang'] = $_GET['lang']; elseif(!isset($_SESSION['lang'])) $_SESSION['lang'] = "dutch"; if($_SESSION['lang']=="dutch") { $langMenu = "menunl"; $langNews = "newsnl"; $langEvent = "eventnl"; $registry['template']->set("newsHeader","Nieuws: "); } elseif($_SESSION['lang']=="english") { $langMenu = "menuen"; $langNews = "newsen"; $langEvent = "eventen"; $registry['template']->set("newsHeader","News: "); } elseif($_SESSION['lang']=="german") { $langMenu = "menudu"; $langNews = "newsdu"; $langEvent = "eventdu"; $registry['template']->set("newsHeader","Nachricht: "); } { $_SESSION['menu_id'] = $_GET['tid']; } # Set menu $sql = "SELECT menu.id menuid, menu.ordermenu menuorder, menu.link menulink, menu.ref menuref, menu2.name menunaam FROM menu JOIN ".$langMenu." as menu2 ON menu.id = menu2.menu_id WHERE menu.publishDate <= DATE(CURRENT_TIMESTAMP) ORDER BY menuref, menuorder"; $statement = $registry['db']->prepare($sql); $statement->execute(); $rows = $statement->fetchAll(); $registry->set("menus",$rows); $registry['template']->set("menus", $rows); # Set admin menu if(isset($_COOKIE['drentheTechniekLogin'])) { $adminMenu = ""; $user = explode(":", $_COOKIE['drentheTechniekLogin']); $result = runQuery("SELECT username, password, type, rights FROM user WHERE username='".$user[0]."' AND password='" .$user[1]. "'",$registry['db']); { if($result[0]['type']=="1") { $adminMenu .= '<li>Beheer <ul> <li><a href="index.php?route=admin/gegevens">Userdata</a></li> <li><a href="index.php?route=admin/userManagement">Usermanagement</a></li> <li><a href="index.php?route=admin/addUser">Add user</a></li> <li><a href="index.php?route=content/addContent">Add content</a></li> <li><a href="index.php?route=content/editContentOverzicht">Contentoverview</a></li> <li><a href="index.php?route=news/addNews">Add news</a></li> <li><a href="index.php?route=events/addEvent">Add event</a></li> <li><a href="index.php?route=admin/metaData">Metadata</a></li> <li><a href="index.php?route=admin/logout">Logout</a></li> </ul></li>'; } elseif($result[0]['type']=="0") { $rights = explode(":", $result[0]['rights']); $adminMenu .= '<li>Beheer <ul> <li><a href="index.php?route=admin/gegevens">Gegevens</a></li>'; if($rights[0]) $adminMenu .= '<li><a href="index.php?route=content/addContent">Add content</a></li>'; $adminMenu .= '<li><a href="index.php?route=content/editContentOverzicht">Contentoverview</a></li>'; if($rights[1]) $adminMenu .= '<li><a href="index.php?route=news/addNews">Add news</a></li>'; if($rights[2]) $adminMenu .= '<li><a href="index.php?route=events/addEvent">Add event</a></li>'; $adminMenu .= '<li><a href="index.php?route=admin/logout">Logout</a></li>'; $adminMenu .= '</ul></li>'; } $registry['template']->set('adminMenu',$adminMenu); } } # set Headers $registry['template']->set("nieuwsteNews",runQuery("SELECT id, title, content FROM ".$langNews." ORDER BY id DESC LIMIT 1",$registry['db'])); $registry['template']->set("nieuwsteEvent",runQuery("SELECT id, startDate, title FROM ". $langEvent ." WHERE enddate >= DATE(NOW()) ORDER BY startDate, startTime LIMIT 1",$registry['db'])); $router->delegate(); $db = NULL; ?>
en mijn router.php als volgt:
<?php
Class Router {
private $registry;
private $path;
private $args = array();
function __construct($registry) {
$this->registry = $registry;
}
function setPath($path) {
$path = trim($path, '/\\');
$path .= DIRSEP;
if (is_dir($path) == false) {
throw new Exception ('Invalid controller path: `' . $path . '`');
}
$this->path = $path;
}
function delegate() {
// Analyze route
$this->getController($file, $controller, $action, $args);
// File available?
if (is_readable($file) == false) {
die ('404 Not Found');
}
// Include the file
include ($file);
// Initiate the class
$class = 'Controller_' . $controller;
$controller = new $class($this->registry);
// Action available?
if (is_callable(array($controller, $action)) == false) {
die ('404 Not Found');
}
// Run action
$controller->$action();
}
private function getController(&$file, &$controller, &$action, &$args) {
$route = (empty($_GET['route'])) ? '' : $_GET['route'];
if (empty($route)) { $route = 'index'; }
// Get separate parts
$route = trim($route, '/\\');
$parts = explode('/', $route);
// Find right controller
$cmd_path = $this->path;
foreach ($parts as $part) {
$fullpath = $cmd_path . $part;
// Is there a dir with this path?
if (is_dir($fullpath)) {
$cmd_path .= $part . DIRSEP;
array_shift($parts);
continue;
}
// Find the file
if (is_file($fullpath . '.php')) {
$controller = $part;
array_shift($parts);
break;
}
}
if (empty($controller)) { $controller = 'index'; };
// Get action
$action = array_shift($parts);
if (empty($action)) { $action = 'index'; }
$file = $cmd_path . $controller . '.php';
$args = $parts;
}
}
?>
<?php Class Router { private $registry; private $path; function __construct($registry) { $this->registry = $registry; } function setPath($path) { $path = trim($path, '/\\'); $path .= DIRSEP; throw new Exception ('Invalid controller path: `' . $path . '`'); } $this->path = $path; } function delegate() { // Analyze route $this->getController($file, $controller, $action, $args); // File available? } // Include the file include ($file); // Initiate the class $class = 'Controller_' . $controller; $controller = new $class($this->registry); // Action available? } // Run action $controller->$action(); } private function getController(&$file, &$controller, &$action, &$args) { $route = (empty($_GET['route'])) ? '' : $_GET['route']; if (empty($route)) { $route = 'index'; } // Get separate parts $route = trim($route, '/\\'); // Find right controller $cmd_path = $this->path; foreach ($parts as $part) { $fullpath = $cmd_path . $part; // Is there a dir with this path? $cmd_path .= $part . DIRSEP; continue; } // Find the file $controller = $part; break; } } if (empty($controller)) { $controller = 'index'; }; // Get action if (empty($action)) { $action = 'index'; } $file = $cmd_path . $controller . '.php'; $args = $parts; } } ?>
Als ik nu op de site wil komen dan gaat hij een tijdje laden en op gegeven moment loopt mijn apache vast:S
Ik gebruik xampp en de nieuwe php versie.
Daarnaast heb ik vista(sucks).
kan iemand helpen ?
|