/** * Adds handler for needed application/controller events * @param \yii\base\Application $app * @param \yii\base\Controller $controller * @return void */ public function subscribe($app, $controller) { $controller->on(Controller::EVENT_PRE_DECORATOR, function ($event) use($controller) { /** @var \app\modules\core\events\ViewEvent $event */ $this->decorate($controller, $event->viewFile, $event->params); }); }
/** * @param string $text * @param array $options * @return mixed|string */ public function prompt($text, $options = []) { if ($this->controller instanceof \yii\console\Controller) { return $this->controller->prompt($text, $options); } else { return isset($options['default']) ? $options['default'] : ''; } }
public function show($type = 'T') { $model = new Controller(); $userMenu = ArrayHelper::map(self::find()->where(['type' => $type])->all(), 'title', 'url'); $pageMenu = ArrayHelper::map(Pages::find()->where(['menu' => $type])->all(), 'title', 'url'); foreach ($pageMenu as $title => $url) { $pageMenu[$title] = Url::toRoute(['/page/view', 'url' => $url]); } $items = array_merge($userMenu, $pageMenu); return $model->renderPartial('/report/menu', ['items' => $items]); }
private static function controllerActions(\yii\base\Controller $controller) { $actions = array_keys($controller->actions()); $reflection = new \ReflectionClass($controller); foreach ($reflection->getMethods() as $method) { if (!preg_match('/^action([A-Z].*)/', $method->name, $matches)) { continue; } $actions[] = self::getRouteName($matches[1]); } return $actions; }
public function runAction($id, $params = []) { // Skip \yii\console\Controller::runAction impl. // Don't care about options and arguments. Just pass the call through // to Doctrine's ConsoleRunner and let it handle everything. return \yii\base\Controller::runAction($id, $params); }
function beforeAction($event) { $this->model_name = ucfirst($_GET['model']); $this->model_class_name = "app\\models\\" . $this->model_name; Yii::$app->response->format = 'json'; return parent::beforeAction($event); }
/** * @param \yii\base\Controller $controller * @return array */ protected function getActions(\yii\base\Controller $controller) { $actions = []; // inline actions $reflection = new \ReflectionObject($controller); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $methods = array_filter($methods, function ($method) { return strpos($method->name, 'action') === 0 && $method->name != 'actions'; }); foreach ($methods as $method) { $actionId = strtolower(preg_replace('/([A-Z]){1}/', '-$1', lcfirst(substr($method->name, 6)))); $dockBlock = null; try { $dockBlock = new DocBlockReflection($method); } catch (\Exception $e) { } $action = new ActionAdapter($controller->createAction($actionId), $dockBlock); $actions[$actionId] = $action; } // external actions foreach ($controller->actions() as $actionId => $alias) { $actions[$actionId] = new ActionAdapter($controller->createAction($actionId)); } return $actions; }
/** * @inheritdoc */ public function init() { $this->_module = Yii::$app->getModule('redactorjs'); if ($this->_module === null) { throw new InvalidConfigException("The module 'redactorjs' was not found. Ensure you have setup the 'redactorjs' module in your Yii configuration file."); } parent::init(); }
/** * @param Controller $controller * * @return array */ public function getControllerActions(Controller $controller) { $actions = array_keys($controller->actions()); $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($method->isPublic() && !$method->isStatic() && mb_strpos($name, self::ACTION_METHOD) === 0 && $name !== 'actions') { if (\Yii::$app->id == $controller->module->id) { continue; } $action = Inflector::camel2id(mb_substr($name, mb_strlen(self::ACTION_METHOD))); $actions[] = $action; } } asort($actions); return $actions; }
public function actionGet_product_view() { if (Yii::$app->request->isAjax) { $id = $_POST['id']; $response['update_view'] = \yii\base\Controller::renderPartial('view', ['model' => $this->findModel($id)]); return json_encode($response); } }
/** * @inheritdoc */ public function beforeAction($action) { if (parent::beforeAction($action)) { if ($this->enableCsrfValidation && Yii::$app->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) { throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.')); } return true; } else { return false; } }
/** * [init description] * @return [type] [description] */ public function init() { parent::init(); // Set theme. // @todo: Move to config. $this->view->theme = new \yii\base\Theme(['pathMap' => ['@app/views' => '@app/themes/klangfarbe'], 'basePath' => '@app/themes/klangfarbe', 'baseUrl' => '@web/themes/klangfarbe']); // Force theming. $this->setViewPath('@app/themes/klangfarbe/' . $this->id); // Define entry point. $this->resolvePathRequested(); }
/** * [init description] * @return [type] [description] */ public function init() { parent::init(); // Set theme. // @todo: Move to config. $this->view->theme = new \yii\base\Theme(['pathMap' => ['@app/views' => '@app/themes/' . \giantbits\crelish\Module::getInstance()->theme], 'basePath' => '@app/themes/' . \giantbits\crelish\Module::getInstance()->theme, 'baseUrl' => '@web/themes/' . \giantbits\crelish\Module::getInstance()->theme]); // Force theming. $this->setViewPath('@app/themes/' . \giantbits\crelish\Module::getInstance()->theme . '/' . $this->id); // Define entry point. $this->resolvePathRequested(); }
/** * Runs an action with the specified action ID and parameters. * If the action ID is empty, the method will use [[defaultAction]]. * @param string $id the ID of the action to be executed. * @param array $params the parameters (name-value pairs) to be passed to the action. * @return integer the status of the action execution. 0 means normal, other values mean abnormal. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. * @see createAction */ public function runAction($id, $params = []) { if (!empty($params)) { $options = $this->globalOptions(); foreach ($params as $name => $value) { if (in_array($name, $options, true)) { $this->{$name} = $value; unset($params[$name]); } } } return parent::runAction($id, $params); }
/** @inheritdoc */ public function runAction($id, $consoleParams = [], $params = []) { if (!empty($consoleParams)) { $options = $this->options($id); foreach ($consoleParams as $name => $value) { if (in_array($name, $options, true)) { $default = $this->{$name}; $this->{$name} = is_array($default) ? preg_split('/\\s*,\\s*/', $value) : $value; unset($consoleParams[$name]); } elseif (!is_int($name)) { throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name])); } } } return BaseController::runAction($id, $params); }
/** * Runs an action with the specified action ID and parameters. * If the action ID is empty, the method will use [[defaultAction]]. * @param string $id the ID of the action to be executed. * @param array $params the parameters (name-value pairs) to be passed to the action. * @return integer the status of the action execution. 0 means normal, other values mean abnormal. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. * @throws Exception if there are unknown options or missing arguments * @see createAction */ public function runAction($id, $params = []) { if (!empty($params)) { // populate options here so that they are available in beforeAction(). $options = $this->options($id); foreach ($params as $name => $value) { if (in_array($name, $options, true)) { $default = $this->{$name}; $this->{$name} = is_array($default) ? preg_split('/\\s*,\\s*/', $value) : $value; unset($params[$name]); } elseif (!is_int($name)) { throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name])); } } } return parent::runAction($id, $params); }
/** * Get route of action * @param \yii\base\Controller $controller * @param array $result all controller action. */ protected function getActionRoutes($controller, &$result) { $token = "Get actions of controller '" . $controller->uniqueId . "'"; Yii::beginProfile($token, __METHOD__); try { $prefix = '/' . $controller->uniqueId . '/'; foreach ($controller->actions() as $id => $value) { $result[$prefix . $id] = $prefix . $id; } $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \\0', substr($name, 6))); $id = $prefix . ltrim(str_replace(' ', '-', $name), '-'); $result[$id] = $id; } } } catch (\Exception $exc) { Yii::error($exc->getMessage(), __METHOD__); } Yii::endProfile($token, __METHOD__); }
/** * Displays an e-mail in preview mode. * @param string $view the view name. Please refer to [[render()]] on how to specify a view name. example: '//mail/register', the view file in backend backend/views folder. * @param array $vars the parameters (name-value pairs) that should be made available in the view. example: ['name' => 'harry', 'link' => 'http://wm.com/XXXX']. * @param string $layout example: '//layouts/email', the view file in backend backend/layouts folder. */ public function setView($view, $vars = array(), $layout = null) { // Get default controller $controller = Yii::$app->controller; if (empty($controller)) { $controller = new Controller('site', Yii::$app->module); } $body = $controller->renderPartial($view, $vars); if ($layout === null) { $this->_view = $body; } else { // Render the layout file with content $this->_view = $controller->renderPartial($layout, array('content' => $body)); } }
public function runAction($id, $params = array()) { $params = array_merge($_POST, $params); return parent::runAction($id, $params); }
/** * * @param \yii\base\Controller $controller * @param Array $result all controller action. */ private static function getActionRoutes($controller, &$result) { $prefix = '/' . $controller->uniqueId . '/'; //print_r(['controllerId'=>$controller->id,'moduleId'=>$controller->module->id]); foreach ($controller->actions() as $id => $value) { //$result[$controller->module->id][$controller->id][] = $id; if (Yii::$app->id == $controller->module->id) { continue; } self::setActionList($id, $result); self::setControllerList($controller, $result); self::setModuleList($controller->module, $result); $result['map'][$controller->module->id][$controller->id][] = $id; /* $result['model'][] = [ 'module' => $controller->module->id, 'controller' => $controller->id, 'action' => $id, ]; //*/ } $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { //$result[$controller->module->id][$controller->id][] = Inflector::camel2id(substr($name, 6)); if (Yii::$app->id == $controller->module->id) { continue; } $action = Inflector::camel2id(substr($name, 6)); self::setActionList($action, $result); self::setControllerList($controller, $result); self::setModuleList($controller->module, $result); $result['map'][$controller->module->id][$controller->id][] = $action; /* $result['model'][] = [ 'module' => $controller->module->id, 'controller' => $controller->id, 'action' => $action, ]; //*/ } } }
/** * Runs an action with the specified action ID and parameters. * If the action ID is empty, the method will use [[defaultAction]]. * @param string $id the ID of the action to be executed. * @param array $params the parameters (name-value pairs) to be passed to the action. * @return integer the status of the action execution. 0 means normal, other values mean abnormal. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. * @throws Exception if there are unknown options or missing arguments * @see createAction */ public function runAction($id, $params = []) { if (!empty($params)) { // populate options here so that they are available in beforeAction(). $options = $this->options($id === '' ? $this->defaultAction : $id); if (isset($params['_aliases'])) { $optionAliases = $this->optionAliases(); foreach ($params['_aliases'] as $name => $value) { if (array_key_exists($name, $optionAliases)) { $params[$optionAliases[$name]] = $value; } else { throw new Exception(Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name])); } } unset($params['_aliases']); } foreach ($params as $name => $value) { if (in_array($name, $options, true)) { $default = $this->{$name}; if (is_array($default)) { $this->{$name} = preg_split('/(?!\\(\\d+)\\s*,\\s*(?!\\d+\\))/', $value); } elseif ($default !== null) { settype($value, gettype($default)); $this->{$name} = $value; } else { $this->{$name} = $value; } $this->_passedOptions[] = $name; unset($params[$name]); } elseif (!is_int($name)) { throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name])); } } } return parent::runAction($id, $params); }
public function init() { MultiLanguageHelper::catchLanguage(); parent::init(); }
public function actionProduct_view() { if (Yii::$app->request->isAjax) { $this->layout = 'blank'; $id = $_POST['id']; $model = ProductCategory::find()->where(['id' => $id])->one(); $data = ProductCategoryRel::find()->where(['category_id' => $id])->orderBy('sort_order', 'DESC')->all(); $response['upload_view'] = \yii\base\Controller::renderPartial('product_list_product_view', ['model' => $model, 'data' => $data]); $response['Category_name'] = $model->cat_title; return json_encode($response); } }
public function behaviors() { if ($this->module->adminId == 'AccessControl') { return ['access' => ['class' => \yii\filters\AccessControl::className(), 'rules' => [['allow' => true, 'roles' => ['@']]]]]; } return parent::behaviors(); }
public function setUp() { $this->mock = $this->getMock(Controller::class, [], ['mock', null]); $this->mock->attachBehavior('responseFormat', ['class' => ResponseFormatBehavior::class]); }
/** * Runs an action with the specified action ID and parameters. * If the action ID is empty, the method will use [[defaultAction]]. * @param string $id the ID of the action to be executed. * @param array $params the parameters (name-value pairs) to be passed to the action. * @return integer the status of the action execution. 0 means normal, other values mean abnormal. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully. * @throws Exception if there are unknown options or missing arguments * @see createAction */ public function runAction($id, $params = []) { if (!empty($params)) { // populate options here so that they are available in beforeAction(). // 获取允许的参数,可以在beforeAction中拿到 $options = $this->options($id); foreach ($params as $name => $value) { if (in_array($name, $options, true)) { // 如果参数在允许的options,才会将值赋给controller的属性 $default = $this->{$name}; // 如果该属性的默认值是数组,就根据逗号和空格(包含" ", \r, \t, \n, \f)的组合分隔value值 $this->{$name} = is_array($default) ? preg_split('/\\s*,\\s*/', $value) : $value; unset($params[$name]); } elseif (!is_int($name)) { throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name])); } } } return parent::runAction($id, $params); }
public function init() { parent::init(); // TODO: Change the autogenerated stub }
public function beforeAction($action) { Yii::$app->response->format = 'json'; return parent::beforeAction($action); }
/** * Get route of action * @param \yii\base\Controller $controller * @param array $result all controller action. */ private function getActionRoutes($controller, &$result) { $token = "Get actions of controller '" . $controller->uniqueId . "'"; Yii::beginProfile($token, __METHOD__); try { $prefix = '/' . $controller->uniqueId . '/'; foreach ($controller->actions() as $id => $value) { $result[] = $prefix . $id; } $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { $result[] = $prefix . Inflector::camel2id(substr($name, 6)); } } } catch (\Exception $exc) { Yii::error($exc->getMessage(), __METHOD__); } Yii::endProfile($token, __METHOD__); }
/** * @param \yii\base\Controller $controller * @param Array $result all controller action. */ private static function getActionRoutes($controller, &$result) { $prefix = '/' . $controller->uniqueId . '/'; foreach ($controller->actions() as $id => $value) { $result[] = $prefix . $id; } $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { $result[] = $prefix . Inflector::camel2id(substr($name, 6)); } } }