<?php namespace Hood; use Yaf\Controller_Abstract; use Hood\Helper\View as hoodView; use Yaf; use Hood\Validator as hoodValidator; class Action extends Controller_Abstract { private $_viewLink = array(); private $_viewScript = array(); private $_headTitle; private $_headmeta; /** * Meta * @return \Hood\Helper\View\Meta */ public function _headMeta() { if (empty($this->_headmeta)) { $this->_headmeta = new hoodView\Meta(); $this->getView()->assign("_headmeta", $this->_headmeta); } return $this->_headmeta; } /** * Script * @return \Hood\Helper\View\Script */ public function _viewScript($scriptName = '_headScript') { if (!isset($this->_viewScript[$scriptName])) { $this->_viewScript[$scriptName] = new hoodView\Script(); $this->getView()->assign($scriptName, $this->_viewScript[$scriptName]); } return $this->_viewScript[$scriptName]; } /** * * @return \Hood\Helper\View\Link */ public function _viewLink($linkName = '_headLink') { if (!isset($this->_viewLink[$linkName])) { $this->_viewLink[$linkName] = new hoodView\Link(); $this->getView()->assign($linkName, $this->_viewLink[$linkName]); } return $this->_viewLink[$linkName]; } /** * Title * @return \Hood\Helper\View\Title */ public function _headTitle($title) { if (empty($this->_headTitle)) { $this->_headTitle = new hoodView\Title(); $this->getView()->assign("_headTitle", $this->_headTitle); } return $this->_headTitle->headTitle($title); } /** * js 跳转 并 提示 * * @param String $url * @param String $expression */ protected function helpJsRedirect($message = '', $script = "history.back();") { $html = ''; if (!empty($message)) { header("content-type: text/html; charset=utf-8"); $message = str_replace("\n", "\\n", $message); $html .= "<script language=\"javascript\">"; $html .= "alert(\"{$message}\");"; $html .= "</script>"; } $html .= "<script language=\"javascript\">"; $html .= $script; $html .= "</script>"; die($html); } /** * 跳转 * @param String $url */ protected function helpLocation($url) { header('Location: ' . $url); } /** * refresh跳转 * @param $url * @param string $message */ protected function helpRefresh($url, $message = '') { $html = ''; if (!empty($message)) { header("content-type: text/html; charset=utf-8"); $message = str_replace("\n", "\\n", $message); $html .= "<script language=\"javascript\">"; $html .= "alert(\"{$message}\");"; $html .= "</script>"; } $html .= "<script language=\"javascript\">"; $html .= "window.location.href='{$url}';"; $html .= "</script>"; echo $html; } /** * JSON输出 * @param $code * @param null $message * @param null $data */ protected function helpJsonResult($code, $message = null, $data = null) { header('Content-type: application/json'); echo json_encode(array('code' => $code, 'message' => $message, 'data' => $data)); exit(); } /** * JSON输出 * @param array $data */ protected function helpJson(array $data) { header('Content-type: application/json'); echo json_encode($data); exit(); } /** * JSONP Callback输出,用于远程调用 * @param $callbackString * @param $code * @param null $message * @param null $data */ protected function helpJsonCallbackResult($callbackString, $code, $message = null, $data = null) { header('Content-type: application/json'); echo $callbackString . "("; echo json_encode(array('code' => $code, 'message' => $message, 'data' => $data)); echo ")"; exit(); } /** * @param string $namespace * @return \Hood\Core\Session\SessionNamespace */ public function session($namespace = 'session_default', $sessionName = null) { return Session::start($namespace, $sessionName); } /** * 数据校验 * @param array $data * @param array $rules * @param array $messagesAttribute * @return Helper\Validation\Validator */ public function validator(array $data, array $rules, array $messagesAttribute = array()) { return hoodValidator::make($data, $rules, $messagesAttribute); } }