Множественные правки во фреймворке - вывод, конфиг, описание методов

This commit is contained in:
Krivchikov Dmitry 2014-04-04 11:06:30 +04:00
parent 2ce0130b7d
commit c92baf7171
6 changed files with 43 additions and 18 deletions

View File

@ -54,12 +54,12 @@ class App
/** /**
* Основной метод для запуска приложения. * Основной метод для запуска приложения.
* @param array $config * @param string $config имя файла с конфигом
*/ */
public static function run($config) public static function run($config = '')
{ {
ob_start(); ob_start();
self::$config = is_array($config) ? $config : array(); self::$config = $config ? include_once $config : array();
self::parseUrl(); self::parseUrl();
if (self::getConfig('DB')) if (self::getConfig('DB'))
@ -90,12 +90,17 @@ class App
public static function error404($message = '') public static function error404($message = '')
{ {
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
die('Error 404 '.$message); die($message ? $message : '404 - Not Found');
} }
public static function redirect($url) /**
* Перенаправление на другой URL
* @param type $url
* @param type $httpCode - по умолчанию 307, временный редирект
*/
public static function redirect($url, $httpCode = 307)
{ {
header("Location: " . $url); header('Location: ' . $url, true, $httpCode);
} }
/** /**

View File

@ -24,6 +24,13 @@ abstract class Controller
} }
} }
/**
* Рендерит шаблон без лейаута.
* @param string $template - имя шаблона
* @param array $data - передаваемые параметры
* @param bool $return - вернуть результат в виде строки
* @return string
*/
static function renderPartial($template, $data, $return = false) static function renderPartial($template, $data, $return = false)
{ {
if (!file_exists(App::getBasedir() . 'views/' . $template)) if (!file_exists(App::getBasedir() . 'views/' . $template))
@ -41,12 +48,18 @@ abstract class Controller
$newContentFormBuffer_temp = ob_get_clean(); $newContentFormBuffer_temp = ob_get_clean();
echo $oldContentFormBuffer_temp; echo $oldContentFormBuffer_temp;
return $newContentFormBuffer_temp; return $newContentFormBuffer_temp;
} else
{
echo $newContentFormBuffer_temp;
}
} }
return true;
}
/**
* Рендерит шаблон и вписывает его в лейаут.
* @param string $template - имя шаблона
* @param array $data - передаваемые параметры
* @param bool $return - вернуть результат в виде строки
* @return string
*/
static function render($template, $data, $return = false) static function render($template, $data, $return = false)
{ {
$content = self::renderPartial($template, $data, true); $content = self::renderPartial($template, $data, true);

View File

@ -77,4 +77,9 @@ class Model
return $res ? $this : FALSE; return $res ? $this : FALSE;
} }
function getValues()
{
return $this->_vals_;
}
} }

View File

@ -1,6 +1,6 @@
<?php <?php
$config = array( return array(
'title' => 'Интересные выходные вместе с wikipoints.ru', 'title' => 'Интересные выходные вместе с wikipoints.ru',
'DB' => array( 'DB' => array(
'host' => 'localhost', 'host' => 'localhost',

View File

@ -15,8 +15,8 @@ class JsonController extends Controller
$categories = $categories->getAll(); $categories = $categories->getAll();
foreach ($points as &$point) { foreach ($points as &$point) {
$point['categoryIcon'] = $categories[$point['categoryId']]['icon']; $point->categoryIcon = $categories[$point->categoryId]['icon'];
$point['categoryName'] = $categories[$point['categoryId']]['name']; $point->categoryName = $categories[$point->categoryId]['name'];
} }
self::renderPartial('json.php', array( self::renderPartial('json.php', array(
@ -38,10 +38,10 @@ class JsonController extends Controller
$categories = new Category(); $categories = new Category();
$categories = $categories->getAll(); $categories = $categories->getAll();
$point['categoryIcon'] = $categories[$point['categoryId']]['icon']; $point->categoryIcon = $categories[$point->categoryId]['icon'];
$point['categoryName'] = $categories[$point['categoryId']]['name']; $point->categoryName = $categories[$point->categoryId]['name'];
self::renderPartial('json.php', $point); self::renderPartial('json.php', $point->getValues());
} else } else
{ {
App::error404(); App::error404();

View File

@ -1,6 +1,8 @@
<?php <?php
include_once dirname(__FILE__) . '/../ground/app.php'; /*
include_once dirname(__FILE__) . '/../ground/config.php'; * Поехали! (с) Ю.А. Гагарин 12.04.1961
*/
App::run($config); include_once dirname(__FILE__) . '/../ground/app.php';
App::run('config.php');