Skip to content

Commit

Permalink
优化控制器基类
Browse files Browse the repository at this point in the history
  • Loading branch information
panco committed Dec 1, 2018
1 parent 2120b8a commit 71f71c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion application/index/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Index extends Controller
*/
public function index()
{
$this->view(Container::template()->fetch("tpl/welcome"));
$this->fetch("tpl/welcome", ["name" => "开发"]);
}

}
44 changes: 37 additions & 7 deletions library/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,51 @@ public function redirect($url, $code = 302)
}

/**
* 返回响应
* 响应文件
* @param $filename
*/
public function sendFile($filename)
{
$this->response($filename);
}

/**
* html响应
* @param $content
*/
public function view($content)
public function fetch($tpl, $array = [])
{
if (is_array($array) && count($array) > 0) {
Container::template()->assign($array);
}
$this->response->end(Container::template()->fetch($tpl));
}

/**
* json响应
* @param $array
*/
public function json($array)
{
$this->response->end($content);
$this->response->end(json_encode($array));
}

/**
* 响应文件
* @param $filename
* xml响应
* @param $array
*/
public function sendFile($filename)
public function xml($array)
{
$this->response($filename);
$xml = "<root>";
foreach ($array as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->xml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</root>";
$this->response->end($xml);
}

}
2 changes: 1 addition & 1 deletion views/tpl/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

</div>
<div class="links">
<a href="https://www.kancloud.cn/panco/panco/860385">文档</a>
<a href="https://www.kancloud.cn/panco/panco/860385">{$name}文档</a>
<a href="https://github.com/panco95/fastSwoole">Github</a>
</div>
</div>
Expand Down

0 comments on commit 71f71c8

Please sign in to comment.