Skip to content

Instantly share code, notes, and snippets.

@TorbenKoehn
Created November 19, 2015 16:15
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 TorbenKoehn/2882684c5ab51dcc2920 to your computer and use it in GitHub Desktop.
Save TorbenKoehn/2882684c5ab51dcc2920 to your computer and use it in GitHub Desktop.
<?php
return [
'db' => [
'host' => 'localhost',
'user' => '<db_user>',
'password' => '<db_password>',
'name' => '<database_name>'
]
];
<?php render_head()?>
<?php render_header()?>
<h1>Page not found!</h1>
<?php render_footer()?>
<?php render_foot()?>
<?php render_head()?>
<?php render_header()?>
<?php switch ($action) {
case 'index':
default:
?>
<h1>My Index Action!</h1>
<p>
This one occurs when you call index.php?controller=book&action=index
</p>
<?php
break;
case 'list':
?>
<h1>Book overview</h1>
<?php
$stmt = $pdo->query('SELECT * FROM `books`');
?>
<?php foreach ($stmt as $row): ?>
<div class="book-row">
<h1><?=$book['title']?></h1>
<p>
<?=$book['teaser']?>
</p>
</div>
<?php endforeach; ?>
<?php
break;
}
?>
<?php render_footer()?>
<?php render_foot()?>
//Some helper functions. E.g.:
function render($view, array $args = null)
{
extract($args ? $args : []);
include VIEW_PATH.'/'.$view.'.phtml';
}
function render_head(array $args = null)
{
return render('head');
}
function render_header(array $args = null)
{
return render('header');
}
function render_footer(array $args = null)
{
return render('footer');
}
function render_foot(array $args = null)
{
return render('foot');
}
<?php
$pdo = new PDO('mysql:host='.$config['db']['host'].'; database='.$config['db]['name'], $config['db']['user'], $config['db']['password']);
//Other initialization logic maybe?
<?php
//Loading the config
$config = include(__DIR__.'/config.php');
//Initializing "controller" and "action" GET variables
$controller = isset($_GET['controller']) ? preg_replace('/[^a-z\-]/i', '', $_GET['controller']) : 'index';
$action = isset($_GET['action']) ? preg_replace('/[^a-z\-]/i', '', $_GET['action']) : 'index';
//Include initialization stuff and helper functions
include __DIR__.'/includes/init.php';
include __DIR__.'/includes/functions.php';
//Check if the given controller is a valid controller
$controllerPath = __DIR__.'/controllers/'.$controller.'.php';
if (!file_exists($controllerPath))
include __DIR__.'/controllers/404.php';
//Include the controller (Meaning "run it")
include $controllerPath;
<script src="jquery.js???"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<!-- Your head content -->
</head>
<body>
<header>
<h1>My Page Header!</h1>
<h2>This should display on all sites</h2>
</header>
/
config.php
index.php
includes/
init.php
functions.php
views/
head.php
header.php
navigation.php
footer.php
foot.php
controllers/
404.php
index.php
book.php
user.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment