Skip to content

Instantly share code, notes, and snippets.

@lcherone lcherone/33.php
Created Jan 18, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.