app = $app; $this->request = $this->app->request; // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { $this->param = $this->request->param(); } // // 以下为新增,为了使用旧版的 success error redirect 跳转 start // /** * 操作成功跳转的快捷方法 * @access protected * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @param array $header 发送的Header信息 * @return void */ protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []) { if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) { $url = $_SERVER["HTTP_REFERER"]; } elseif ($url) { $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url); } $result = [ 'code' => 0, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait, ]; $type = $this->getResponseType(); if ($type == 'html') { $response = view($this->app->config->get('app.dispatch_success_tmpl'), $result); } else if ($type == 'json') { $response = json($result); } throw new HttpResponseException($response); } /** * 操作错误跳转的快捷方法 * @access protected * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @param array $header 发送的Header信息 * @return void */ protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []) { if (is_null($url)) { $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);'; } elseif ($url) { $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url); } $result = [ 'code' => 1, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait, ]; $type = $this->getResponseType(); if ($type == 'html') { $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result); } else if ($type == 'json') { $response = json($result); } throw new HttpResponseException($response); } /** * URL重定向 自带重定向无效 * @access protected * @param string $url 跳转的URL表达式 * @param array|integer $params 其它URL参数 * @param integer $code http code * @param array $with 隐式传参 * @return void */ protected function redirect($url, $params = [], $code = 302, $with = []) { $response = Response::create($url, 'redirect'); if (is_integer($params)) { $code = $params; $params = []; } $response->code($code)->params($params)->with($with); throw new HttpResponseException($response); } /** * 获取当前的response 输出类型 * @access protected * @return string */ protected function getResponseType() { return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html'; } // // 以上为新增,为了使用旧版的 success error redirect 跳转 end // }