// +---------------------------------------------------------------------- namespace app\http\middleware; use think\facade\View; /** * @title 后台中间件 */ class Front { protected $data = []; public function handle($request, \Closure $next) { $response = $next($request); $this->data = $response->getData(); if ($request->isAjax()) { return json($this->data); }else{ return $response->data($this->fetch()); } } /** * @title 显示类 */ protected function fetch($template = ''){ // 使用内置PHP模板引擎渲染模板输出 $config = array( 'tpl_replace_string' => array( '__static__' => '/static', '__img__' => '/static/front/images', '__css__' => '/static/front/css', '__js__' => '/static/front/js', '__public__' => '/static/front', ) ); if (is_string($this->data)) { $this->data = array('data' => $this->data); } View::config($config); View::assign($this->data); return View::engine('Think')->fetch($template); } }