Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
require_once('../framework/Main.php');
require_once('../application/config/config.php');
$routes = array(
'/blog/get/(\d+)' => array('controller' => 'blog', 'action' => 'get', 'params' => array(1 => 'id')),
);
Main::setRoot(dirname(__FILE__) . '/../');
Main::setRoutes($routes);
arr2obj($config);
Registry::set('config', $config);
try
{
Main::run();
} catch (Exception $e)
{
header("HTTP/1.0 404 Not Found");
include('404.html');
}
private function __construct()
{
spl_autoload_register(array($this, 'loader'));
}
private function loader($class)
{
$incPath = get_include_path();
$end_ptr = strlen($class) - strlen('controller');
if ($end_ptr != 0 && strcasecmp(substr($class, $end_ptr), 'controller') === 0)
set_include_path($this->_root . $this->_controllerPath . PATH_SEPARATOR . $incPath);
else
set_include_path($this->_root . $this->_modelsPath . PATH_SEPARATOR .
$this->_root . $this->_libraryPath . PATH_SEPARATOR . $incPath);
$filename = $class . '.php';
$f = fopen($filename, 'r', true);
if (!$f)
{
set_include_path($incPath);
throw new Exception('Class not found.');
}
fclose($f);
include $filename;
set_include_path($incPath);
}
function Request($method, $path, $callback)
{
return new Request($method, $path, $callback);
}
class Request {
public static function factory($method, $path, $callback)
{
return new self($method, $path, $callback);
}
...
}global $application;
class Application
{
private function i_run()
{
....
}
public static function run()
{
return Application::getInstance()->i_run();
}
}
Application::getInstance()->run(); Application::run(); Request('get', '/user/{username}/profile', UserProfile)->assert('username', '|^[\w\d]+$|');
Request('get', '/shop/{id}/rating', ShopItemRating)->assert('id', '|^\d+$|');
Application::run(Request::factory('/user/{id}', UserProfile)->assert('|^\d+$|')->run())
// Parse query
// host/dir/subdir/../subdirX/?param1=1¶m2=2&...¶mX=x
// $url_array[]
// $url_array[0] = "dir";
// $url_array[1] = "subdir";
// ..
// $url_array[X] = "subdirX";
// $_GET["param1"] = "1";
// $_GET["param2"] = "2";
// ..
// $_GET["paramX"] = "X";
$url = "http://localhost".$_SERVER['REQUEST_URI'];
$temp_url = parse_url($url);
$dirs = explode('/', $temp_url['path']);
isset($temp_url['query']) && parse_str($temp_url['query'], $_GET);
/hello_{name} оно распознает? Или параметр обязательно должен быть окружен косыми чертами? И как с этим обстоит дело в Silex?if (!class_exists('Application'))
{ ...
class Request {...}
class Application {...}
Application::init();
}else
Application::run();
01 $app = new Application( );
02 if ( $app->isInit() )
03 $app->run();
04 else
05 $app->init( new Request());
public function init() {
$this->request = new Request();
$this->config = new Config();
$this->config->load('main');
$this->db = new Db( $this->config->db );
// и тд
}
public function run() {
$this->init();
...
}
private function init() {
// используем ленивую проверку
if (! $thid->db)
$this->db=new Db( $this->config->db );
}
Мини-фреймворк своими руками