/** * 路由结束之后触发 此时路由一定正确完成, 否则这个事件不会触发 * * @param Request_Abstract $request * @param Response_Abstract $response * @return mixed|void */ public function routerShutdown(Request_Abstract $request, Response_Abstract $response) { $config = Registry::get("config")->smarty->toArray(); $config['template_dir'] = $config['template_dir'] . $request->module . '/'; $smarty = new Smarty\Adapter(null, $config); Dispatcher::getInstance()->setView($smarty); }
/** * ServiceApi初始化 */ public function init() { Dispatcher::getInstance()->returnResponse(true); Dispatcher::getInstance()->disableView(); $this->_config = Registry::get('config'); $this->_initRequestData(); }
/** * Initialize custom exception handler. * @param \Yaf\Dispatcher $dispatcher * @return void */ public function _initException(\Yaf\Dispatcher $dispatcher) { // 抛出异常,不使用\Yaf\ErrorController接收,通过\Core\ExceptionHandler处理 \Yaf\Dispatcher::getInstance()->throwException(true); \Yaf\Dispatcher::getInstance()->catchException(false); new \Core\ExceptionHandler(); }
public function errorAction($exception) { // $this->getView()->setLayout(null); // fallback views path to global when error occured in modules. $config = Yaf\Application::app()->getConfig(); $this->getView()->setScriptPath($config->application->directory . "/views"); $this->getView()->e = $exception; $this->getView()->e_class = get_class($exception); $this->getView()->e_string_trace = $exception->getTraceAsString(); $params = $this->getRequest()->getParams(); unset($params['exception']); $this->getView()->params = array_merge(array(), $params, $this->getRequest()->getPost(), $this->getRequest()->getQuery()); $logFileName = 'error_' . date('Ymd'); eYaf\Logger::getLogger($logFileName)->logException($exception); switch ($exception->getCode()) { case \YAF\ERR\AUTOLOAD_FAILED: case \YAF\ERR\NOTFOUND\MODULE: case \YAF\ERR\NOTFOUND\CONTROLLER: case \YAF\ERR\NOTFOUND\ACTION: header('HTTP/1.1 404 Not Found'); break; case 401: $this->forward('Index', 'application', 'accessDenied'); header('HTTP/1.1 401 Unauthorized'); \Yaf\Dispatcher::getInstance()->disableView(); echo $this->render('accessdenied'); break; default: header("HTTP/1.1 500 Internal Server Error"); break; } eYaf\Logger::stopLogging($logFileName); }
public function _initRoute(\Yaf\Dispatcher $dispatcher) { $router = \Yaf\Dispatcher::getInstance()->getRouter(); //创建一个路由协议实例 $route = new \Yaf\Route\Rewrite('exp/:ident', array('controller' => 'index', 'action' => 'index')); //使用路由器装载路由协议 $router->addRoute('exp', $route); }
/** * * @return string */ function getClientIp() { $request = Dispatcher::getInstance()->getRequest(); if ($request->isCli()) { } else { return $request->getServer('REMOTE_ADDR'); } }
public function _initRoute(Dispatcher $dispatcher) { //在这里注册自己的路由协议,默认使用简单路由 $router = Dispatcher::getInstance()->getRouter(); /** * add the routes defined in ini config file */ $router->addConfig(Registry::get("config")->routes); }
/** * setup yaf */ private function __setUpYafApplication() { $this->__setUpPHPIniVariables(); // Import application and bootstrap. \Yaf\Loader::import(dirname(__DIR__) . '/public/index.php'); $this->__setUpApplicationInit(); \Yaf\Dispatcher::getInstance()->registerPlugin(new \YafUnit\Plugin\View()); \Yaf\Registry::set('ApplicationInit', true); }
/** * @param Simple $View * @return bool */ public function setView(Simple $View) { if (APPLICATION_IS_CLI) { return false; } $this->_coreView = $View; Dispatcher::getInstance()->setView($View); return true; }
/** * 初始化ServiceJob */ public function init() { Dispatcher::getInstance()->disableView(); if (strtolower($this->getRequest()->getMethod()) !== 'cli') { $sender = new SenderHttp(); $sender->setStatus(503); $sender->send(); } }
/** * 路由结束之后触发 此时路由一定正确完成, 否则这个事件不会触发 * * @param Request_Abstract $request * @param Response_Abstract $response * @return mixed|void */ public function routerShutdown(Request_Abstract $request, Response_Abstract $response) { if ($this->_checkModuleTpl($request->module)) { //只有这些模板才注入 $config = Registry::get("config")->smarty->toArray(); $config['template_dir'] = $config['template_dir'] . $request->module . '/'; $smarty = new TheFairLib\Smarty\Adapter(null, $config); Dispatcher::getInstance()->setView($smarty); } }
public function init() { //do not call render for ajax request if ($this->getRequest()->isXmlHttpRequest()) { \Yaf\Dispatcher::getInstance()->autoRender(FALSE); } //use layout if ($this->layout) { $this->setLayout($this->layout); } }
/** * Constructor. * * @param array $config Configuration key-value pairs. */ public function __construct($config) { // set template path $this->witty = new witty(); //初始化目录 $client = client::getDevice(); $model = $client == 'pc' ? '/pc/' : '/mobile/'; $tpl_dir = $config['tpl_dir'] . $model; $parse_file = $config['compile_dir'] . $model . \Yaf\Dispatcher::getInstance()->getRequest()->getControllerName() . '/'; $this->witty->setTplDir($tpl_dir); $this->witty->setLayoutDir($tpl_dir . '/layout/'); $this->witty->setCompileDir($parse_file); $this->witty->setCacheDir($config['cache_dir']); //$this->_options = $options; }
/** * @brief 调试输出变量 * * @param ...$params * * @return void */ public static function vars(...$params) { $isCli = \Yaf\Dispatcher::getInstance()->getRequest()->isCli(); echo $isCli ? '' : '<br>'; echo '*****************************DEBUG::VARS******************************'; echo $isCli ? PHP_EOL : '<br>' . PHP_EOL; echo $isCli ? '' : '<pre>' . PHP_EOL; foreach ($params as $item) { var_dump($item); } $trace = debug_backtrace(); echo $isCli ? '' : '</pre>' . PHP_EOL; echo "from: [file: {$trace[0]['file']} line: {$trace[0]['line']}]"; echo $isCli ? PHP_EOL : PHP_EOL . '<br>'; echo '**********************************************************************'; echo $isCli ? PHP_EOL : '<br>'; }
/** * 响应头 * @param * $type */ public static function setHeader($type = '') { if (\Yaf\Dispatcher::getInstance()->getRequest()->isCli()) { return false; } switch ($type) { case 'json': header('Content-Type:application/json;charset=utf-8'); header('Pragma: no-cache'); header('Cache-Control: no-cache, no-store, max-age=0'); header('Expires: 1L'); break; default: header('Content-type:text/html;charset=utf-8'); break; } }
/** * 导出API的api方法 * * @access public * @param string $module 应用模块名 * @param string $controller 对应模块内的控制器 * @param string $action 对应控制器中的动作名 * @param mixed $parameters 请求传递的参数 * @return string API调用的响应正文 */ public function api($module, $controller, $action, $parameters) { try { $app = new Application(INI_PATH, 'product'); $request = new RequestSimple('API', $module, $controller, $action, $parameters); $response = $app->bootstrap()->getDispatcher()->dispatch($request); return $response->getBody(); } catch (Exception $e) { if (Application::app()->getConfig()->application->queue->log->switch) { $error = new ErrorLog($e, Dispatcher::getInstance()->getRequest()); $error->errorLog(); } $error = explode(ES, $e->getMessage(), 2); if (isset($error[1])) { return $error[1]; } } }
/** * 生成指定路由类型url * 如果需要根据路由动态生成url,请调用Yaf_route_interface::assemble方法(注意这个assemble方法在低版本下有问题,参考github issue) * * @param $m * @param $c * @param $a * @param array $params */ public static function url($c, $a = 'index', $m = 'default', array $params = [], $routerType = 'default') { if ($m == 'default') { $resquest = \Yaf\Dispatcher::getInstance()->getRequest(); $m = strtolower($resquest->getModuleName()); } /** * 默认静态路由方式 */ if ($routerType == 'default') { $queryString = ''; foreach ($params as $key => $item) { $queryString .= '/' . $key . '/' . $item; } return '/' . implode('/', [$m, $c, $a]) . $queryString; } //TODO: 按需求补全其它路由 throw new \Yaf\Exception\RouterFailed('无效的路由类型' . $routerType . '无法生成URL'); }
public function __construct($views = null, $extraParams = array()) { $this->_smarty = new \Smarty(); foreach ($extraParams as $key => $value) { $this->_smarty->{$key} = $value; } //根据不同的模块设置不同的模版路径 $dispatcher = Dispatcher::getInstance(); $arrRequest = $dispatcher->getRequest(); if ($views == null) { $this->_views = "views"; } else { $this->_views = $views; } if (empty($arrRequest->module) or 'index' == strtolower($arrRequest->module)) { $this->_script_path = APPLICATION_PATH . '/application/' . $this->_views . '/' . strtolower($arrRequest->controller); $this->setScriptPath(APPLICATION_PATH . '/application/' . $this->_views . '/' . strtolower($arrRequest->controller)); } else { $this->_script_path = APPLICATION_PATH . '/application/modules/' . $arrRequest->module . '/' . $this->_views; $this->setScriptPath(APPLICATION_PATH . '/application/modules/' . $arrRequest->module . '/' . $this->_views); } }
function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response) { $dispatcher = Dispatcher::getInstance(); $controllerName = strtolower($request->getControllerName()); $isApi = 0 === strpos($controllerName, 'api'); $isCli = 0 === strpos($controllerName, 'cli_'); // db \Zend_Db_Table::setDefaultAdapter(AbstractModel::getInstance()->getMultiDb()->getDefaultDb()); // view if ($isApi) { $dispatcher->disableView(); } else { if ($response instanceof Http) { $response->setHeader('Content-Type', 'text/html; charset=UTF-8'); } $dispatcher->setView(SmartyView::getInstance()); } // cli if ($isCli && !$request->isCli()) { $request->setControllerName('Error')->setActionName('error403'); } }
protected function checkUserInput($param, $method) { /* @var \Yaf\Request\Http $request */ $request = Dispatcher::getInstance()->getRequest(); if ($method != null && $method != $request->getMethod()) { return new ApiResponse(Code::FAIL_REQUEST_METHOD, $method . ' support only'); } if ($param) { foreach ($param as $k => $v) { if (is_int($k) && $k <= self::TYPE_MAX) { switch ($k) { case self::LONG: if (!is_long($v)) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be long'); } break; case self::INT: if (!is_int($v)) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be integer'); } break; case self::UNSIGNED_INT: if (!is_int($v) || $v < 0) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be unsigned integer'); } break; case self::STRING: if (!is_string($v)) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be string'); } break; case self::FLOAT: if (!is_float($v)) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float'); } break; case self::DOUBLE: if (!is_double($v)) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float'); } break; case self::FILE: if ($request->getFiles($v) === null) { return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be file'); } break; default: return new ApiResponse(Code::FAIL_PARAMETER_TYPE, 'type of ' . $v . ' not support'); } } else { if (ApiRequest::getParam($v) === null) { return new ApiResponse(Code::FAIL_PARAMETER_MISSING, 'parameter ' . $v . ' missing'); } } } } return null; }
public function init() { Dispatcher::getInstance()->returnResponse(true); Dispatcher::getInstance()->disableView(); }
/** * ServiceApi初始化 */ public function init() { Dispatcher::getInstance()->returnResponse(true); Dispatcher::getInstance()->disableView(); $this->_output_format = $this->getRequest()->getParam('format'); }
/** * Create a HTML page link. * * @param int $page * @param string $text * @param string $class * @return string */ protected function link($page, $text, $class) { $query = '?page=' . $page . $this->appendage($this->appends); $uri = \Yaf\Dispatcher::getInstance()->getRequest()->getRequestUri(); return Html::linkTo($text, $uri . $query, compact('class')); }
public function indexAction() { Dispatcher::getInstance()->disableView(0); echo 'Great,It Works!'; }
/** * 测试redis pub/sub */ public function subAction() { Dispatcher::getInstance()->disableView(); $redis = Registry::get('mount')->get('redis')->getResource(); $redis->subscribe(array('log'), array($this, 'echoData')); }
/** * 通过调度器initView方法可以返回\Core\View核心视图对象 * @return \Yaf\View_Interface $view 视图对象 */ public function getView() { return \Yaf\Dispatcher::getInstance()->initView(null); }
public function init() { \Yaf\Dispatcher::getInstance()->disableView(); }
/** * 初始化控制器 */ public function init() { //禁止自动加载模板,需要手工指定模板路径 Dispatcher::getInstance()->autoRender(false); }
/** * 关闭模板的默认渲染设置 * * @return bool */ public function disableView() { return \Yaf\Dispatcher::getInstance()->disableView(); }
/** * Service初始化 */ public function init() { $this->request_http = new Request_Http(); $this->output_format = $this->request_http->getPost('format'); Dispatcher::getInstance()->disableView(); }