Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
1. header send change
2. json header send and error
3. exception out put
4. post row body
  • Loading branch information
hisune committed Dec 2, 2015
1 parent 2323259 commit 9ccceda
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Tiny/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public function __construct($route)

public function controller()
{
session_start();
headers_sent() OR header('Content-Type: text/html; charset=utf-8');
@session_start();

if(php_sapi_name() == 'cli'){
list(Request::$controller, Request::$method, Request::$params) = $this->cliRoute();
Expand Down
2 changes: 2 additions & 0 deletions Tiny/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function printException($e, $addOn = null)
{
$detail = Html::tag('b', get_class($e), array('style' => 'color: #990000'));
if(Config::config()->show_error){
headers_sent() OR header('HTTP/1.0 500 Internal Server Error');
$detail .= Html::tag('p', $e->getMessage());
$detail .= Html::tag('p', Html::tag('b', $e->getFile()) . '(line ' . $e->getLine() . ')');
$addOn && $detail .= HTML::tag('p', $addOn);
Expand Down Expand Up @@ -65,6 +66,7 @@ public static function print404()

public static function echoJson($status, $data = '', $exit = true, $writeLog = false, $writeMessage = null)
{
headers_sent() || header('Content-Type: application/json; charset=utf-8');
echo json_encode(array('status' => $status, 'data' => $data));
if($writeLog){
if(!$writeMessage)
Expand Down
10 changes: 3 additions & 7 deletions Tiny/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
class Exception
{

public static function header()
{
headers_sent() OR header('HTTP/1.0 500 Internal Server Error');
}

public static function fatal()
{
if($e = error_get_last())
Expand All @@ -28,15 +23,16 @@ public static function exception(\Exception $e, $addOn = null)

try{
Error::logMessage($message);
self::header();

Error::printException($e, $addOn);
}catch(\Exception $e){
print $message;
if(Config::config()->show_error)
print $message;
}

exit(1);
}

}

// END
6 changes: 5 additions & 1 deletion Tiny/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Request
public static $controller;
public static $method;
public static $params;
public static $rowBody = null;

/**
* Is this a GET request?
Expand Down Expand Up @@ -63,7 +64,10 @@ public static function server($key = null)

public static function rawBody()
{
return file_get_contents('php://input');
if(is_null(static::$rowBody))
static::$rowBody = file_get_contents('php://input');

return static::$rowBody;
}

// 当前请求的url
Expand Down
1 change: 1 addition & 0 deletions Tiny/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function display($tpl = '')
}else
$content = $this->_getContent($tpl);
$this->addToken($content);
headers_sent() || header('Content-Type: text/html; charset=utf-8');
echo $content;
}

Expand Down

0 comments on commit 9ccceda

Please sign in to comment.