Created
January 18, 2018 17:23
-
-
Save lcherone/74c17dcee670392ff047b008b91cc355 to your computer and use it in GitHub Desktop.
Tiny procedural MVC example, with template and nested views.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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