mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进Response类 改进view助手函数 增加 think\response\Html 类
This commit is contained in:
30
helper.php
30
helper.php
@@ -340,14 +340,14 @@ function trace($log = '[think]', $level = 'log')
|
||||
* 渲染模板输出
|
||||
* @param string $template 模板文件
|
||||
* @param array $vars 模板变量
|
||||
* @param integer $code 状态码
|
||||
* @param string $type 输出类型
|
||||
* @return \think\Response
|
||||
*/
|
||||
function view($template = '', $vars = [], $type = 'html')
|
||||
function view($template = '', $vars = [], $code = 200)
|
||||
{
|
||||
$data = View::instance(Config::get('template'), Config::get('view_replace_str'))->fetch($template, $vars);
|
||||
$response = Response::create($type);
|
||||
return $response->data($data);
|
||||
$response = new \think\response\Html();
|
||||
return $response->data($template)->vars($vars)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,10 +386,26 @@ function response($type = '', $options = [])
|
||||
|
||||
/**
|
||||
* 获取\think\response\Json对象实例
|
||||
* @param array $options 参数
|
||||
* @param mixed $data 返回的数据
|
||||
* @param integer $code 状态码
|
||||
* @param array $options 参数状
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
function json($options = [])
|
||||
function json($data = [], $code = 200, $options = [])
|
||||
{
|
||||
return new \think\response\Json($options);
|
||||
$response = new \think\response\Json($options);
|
||||
return $response->data($data)->code($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取\think\response\Redirect对象实例
|
||||
* @param mixed $url 重定向地址 支持Url::build方法的地址
|
||||
* @param integer $code 状态码
|
||||
* @param array $params 额外参数
|
||||
* @return \think\response\Redirect
|
||||
*/
|
||||
function redirect($url = [], $code = 200, $params = [])
|
||||
{
|
||||
$response = new \think\response\Redirect();
|
||||
return $response->data($url)->code($code)->params($params);
|
||||
}
|
||||
|
||||
@@ -297,4 +297,13 @@ class Response
|
||||
{
|
||||
return !empty($name) ? $this->header[$name] : $this->header;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +95,13 @@ class View
|
||||
* 解析和获取模板内容 用于输出
|
||||
* @param string $template 模板文件名或者内容
|
||||
* @param array $vars 模板输出变量
|
||||
* @param array $replace 替换内容
|
||||
* @param array $config 模板参数
|
||||
* @param bool $renderContent 是否渲染内容
|
||||
* @param bool $renderContent 是否渲染内容
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetch($template = '', $vars = [], $config = [], $renderContent = false)
|
||||
public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false)
|
||||
{
|
||||
// 模板变量
|
||||
$vars = array_merge($this->data, $vars);
|
||||
@@ -118,8 +119,9 @@ class View
|
||||
// 内容过滤标签
|
||||
APP_HOOK && Hook::listen('view_filter', $content);
|
||||
// 允许用户自定义模板的字符串替换
|
||||
if (!empty($this->replace)) {
|
||||
$content = strtr($content, $this->replace);
|
||||
$replace = array_merge($this->replace, $replace);
|
||||
if (!empty($replace)) {
|
||||
$content = strtr($content, $replace);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
@@ -146,12 +148,13 @@ class View
|
||||
* @access public
|
||||
* @param string $content 内容
|
||||
* @param array $vars 模板输出变量
|
||||
* @param array $replace 替换内容
|
||||
* @param array $config 模板参数
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($content, $vars = [], $config = [])
|
||||
public function display($content, $vars = [], $replace = [], $config = [])
|
||||
{
|
||||
return $this->fetch($content, $vars, $config, true);
|
||||
return $this->fetch($content, $vars, $replace, $config, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
85
library/think/response/Html.php
Normal file
85
library/think/response/Html.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Config;
|
||||
use think\Response;
|
||||
use think\View;
|
||||
|
||||
class Html extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [];
|
||||
protected $vars = [];
|
||||
protected $replace = [];
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
return View::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch($data, $this->vars, $this->replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图变量赋值
|
||||
* @access protected
|
||||
* @param array $vars 模板变量
|
||||
* @return $this
|
||||
*/
|
||||
public function vars($vars = [])
|
||||
{
|
||||
$this->vars = $vars;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
* @access public
|
||||
* @param mixed $name 变量名
|
||||
* @param mixed $value 变量值
|
||||
* @return $this
|
||||
*/
|
||||
public function assign($name, $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
$this->vars = array_merge($this->vars, $name);
|
||||
return $this;
|
||||
} else {
|
||||
$this->vars[$name] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图内容替换
|
||||
* @access public
|
||||
* @param string|array $content 被替换内容(支持批量替换)
|
||||
* @param string $replace 替换内容
|
||||
* @return $this
|
||||
*/
|
||||
public function replace($content, $replace = '')
|
||||
{
|
||||
if (is_array($content)) {
|
||||
$this->replace = array_merge($this->replace, $content);
|
||||
} else {
|
||||
$this->replace[$content] = $replace;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,10 +17,10 @@ use think\Url;
|
||||
class Redirect extends Response
|
||||
{
|
||||
|
||||
protected $options = [
|
||||
'http_response_code' => 301,
|
||||
'http_url_params' => [],
|
||||
];
|
||||
protected $options = [];
|
||||
|
||||
// URL参数
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
@@ -31,10 +31,15 @@ class Redirect extends Response
|
||||
protected function output($data)
|
||||
{
|
||||
$this->isExit = true;
|
||||
$url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->options['http_url_params']);
|
||||
$url = preg_match('/^(https?:|\/)/', $data) ? $data : Url::build($data, $this->params);
|
||||
$this->header['Location'] = $url;
|
||||
$this->header['status'] = $this->options['http_response_code'];
|
||||
$this->header['status'] = isset($this->header['status']) ? $this->header['status'] : 301;
|
||||
return;
|
||||
}
|
||||
|
||||
public function params($params = [])
|
||||
{
|
||||
$this->params = $params;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,12 +104,13 @@ trait Jump
|
||||
* URL重定向
|
||||
* @access protected
|
||||
* @param string $url 跳转的URL表达式
|
||||
* @param array|int $params 其它URL参数或http code
|
||||
* @param integer $code http code
|
||||
* @param array $params 其它URL参数
|
||||
* @return void
|
||||
*/
|
||||
public function redirect($url, $params = [])
|
||||
public function redirect($url, $code = 301, $params = [])
|
||||
{
|
||||
Response::create()->isExit(true)->redirect($url, $params);
|
||||
Response::create('redirect')->data($url)->code($code)->params($params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user