Witam, napisałem właśnie do swojego frameworka Front Controller i Router. Chciałbym prosić was o opinię co mógł bym z optymalizować lub poprawić. Wiem, że człowiek nie jest nieomylny, dlatego wasze spostrzeżenia będą dla mnie bardzo cenne.
frontcontroller.php<?php defined('BASEPATH') OR
exit('No direct script access allowed.');
require_once SYS_PATH . 'core/controller.php';
require_once SYS_PATH . 'core/router.php';
class Bootstrap
{
public function init(Router $router)
{
$controller = $router->getController();
$method = $router->getAction();
$params = $router->getParams();
require_once APP_PATH . 'config/config.php';
$controller = $controller ? $controller : DEFAULT_CONTROLLER;
$method = $method ? $method : DEFAULT_ACTION;
$path_controller = APP_PATH . 'controllers/' . $controller . '.php';
require_once $path_controller;
$controller = new $controller();
if (method_exists($controller, $method)) {
return call_user_func
(array($controller, $method), $params); }
echo '404 not found - method'; } else {
echo '404 not found - controller'; }
}
}
router.php<?php defined('BASEPATH') OR
exit('No direct script access allowed.');
class Router
{
protected $controller;
protected $action;
protected $params;
public function __construct()
{
$path_info = !empty($_SERVER['PATH_INFO']) ?
$_SERVER['PATH_INFO'] : (!empty($_SERVER['ORIG_PATH_INFO']) ?
$_SERVER['ORIG_PATH_INFO'] : '');
$origin = implode('/', $path_info);
require APP_PATH . 'config/routes.php';
foreach ($route as $key => $value) {
array(':any', ':num', ':nonum', ':alpha', ':alnum', ':hex'), array('[^/]+', '[0-9]+', '[^0-9]+', '[A-Za-z]+', '[A-Za-z0-9]+', '[A-Fa-f0-9]+'), $key
);
$origin = preg_replace('#^' . $key . '$#', $value, $origin); }
}
$this->controller = ($c = array_shift($parts)) ?
$c : 'welcome'; $this->action = ($c = array_shift($parts)) ?
$c : 'index';
// Params
$this->params = (isset($parts2[2
])) ?
$parts2[2
] : null;
//echo $this->getController() . ',' . $this->getAction() . ',' . $this->getParams() . '<br />';
}
public function getController()
{
return $this->controller;
}
public function getAction()
{
return $this->action;
}
public function getParams()
{
return $this->params;
}
}
Ten post edytował walus16 10.10.2014, 07:35:22