Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created January 18, 2018 17:23
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 lcherone/74c17dcee670392ff047b008b91cc355 to your computer and use it in GitHub Desktop.
Save lcherone/74c17dcee670392ff047b008b91cc355 to your computer and use it in GitHub Desktop.
Tiny procedural MVC example, with template and nested views.
<?php
// passed from rewrite
$_GET['controller'] = ''; // index page
// controllers map
$controllers = [
'' => 'index.php',
'foobar' => 'foobar.php'
];
// mock a view - would use ob_start() ob_end_clean().
function view($template, $vars) {
return 'Loaded: '.$template.' - with: '.print_r($vars, true).PHP_EOL;
}
// mock some vars (model)
$vars = [
'result' => ['foo'=>'bar']
];
// simplist form of a router
// check the file_exists not !empty() then based upon that show a 404 page/controller
echo !empty($controllers[$_GET['page']]) ? view(
'template.php',
$vars+[
'body' => view('./controller/'.$controllers[$_GET['page']].'.php', $vars)
]
) : view(
'template.php',
$vars+[
'body' => view('./controller/not_found.php', [])
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment