Last active
October 9, 2015 20:38
-
-
Save IsTheJack/ad5569ab96e6f0814030 to your computer and use it in GitHub Desktop.
Exemplo de front controller que cria um modelo de rotas baseando em um modelo de mvc simplificado.
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 | |
/** | |
* Leve em consideração que vc têm o htaccess dessa forma: | |
* RewriteEngine on | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} !-d | |
* RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA] | |
*/ | |
/** | |
* Este arquivo é responsável pela configuração das constantes do projeto e do roteamento | |
*/ | |
define('BASE_DIR', __DIR__ . DIRECTORY_SEPARATOR); | |
// Carregamentos Primordiais (Funções de Caminho e de Carregamento) | |
include BASE_DIR . '_core/helper/path.php'; | |
include BASE_DIR . '_core/helper/loader.php'; | |
include '_app/config/autoload.php'; | |
// Configuração de projeto | |
include BASE_DIR . '_app/config/project.php'; | |
var_dump($configProject); | |
// Roteamento Base : (controler/método/(parametros/...)) | |
$url['full'] = explode('/', filter_input(INPUT_GET, 'params')); | |
$url['class'] = ( isset($url['full'][0]) && !empty($url['full'][0]) )? ucfirst($url['full'][0]) : ucfirst($configProject['default_controller']); | |
$url['method'] = ( isset($url['full'][1]) && !empty($url['full'][1]) )? $url['full'][1] : $configProject['default_method']; | |
if ( isset($url['full'][0]) && !empty($url['full'][0]) && isset($url['full'][1]) && !empty($url['full'][1]) ) | |
$url['params'] = (array) explode('/', str_replace($url['full'][0] . '/' . $url['full'][1] . '/', '', filter_input(INPUT_GET, 'params'))); | |
var_dump($url['params']); | |
// Aplicando as classes Model ((PENDENTE)) e Controller para serem herdadas | |
require BASE_DIR . '_core/c/controller.php'; | |
includeController(strtolower($url['class'])); | |
$refClass = new ReflectionClass($url['class']); | |
$controller = $refClass->newInstance(); | |
// E lá vamos nós... | |
call_user_func_array(array($controller, $url['method']), $url['params']); | |
unset($url['full']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment