From b77ea4dcb0f9270ff353bfe2c8e0fcc67af757e9 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 4 Sep 2013 21:03:10 -0400 Subject: [PATCH] Allow generator to define actions. --- .../yii/gii/controllers/DefaultController.php | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/framework/yii/gii/controllers/DefaultController.php b/framework/yii/gii/controllers/DefaultController.php index 969096b..305ef35 100644 --- a/framework/yii/gii/controllers/DefaultController.php +++ b/framework/yii/gii/controllers/DefaultController.php @@ -7,6 +7,7 @@ namespace yii\gii\controllers; +use Yii; use yii\web\Controller; use yii\web\HttpException; @@ -86,6 +87,26 @@ class DefaultController extends Controller throw new HttpException(404, "Code file not found: $file"); } + /** + * Runs an action defined in the generator. + * Given an action named "xyz", the method "actionXyz()" in the generator will be called. + * If the method does not exist, a 400 HTTP exception will be thrown. + * @param string $id the ID of the generator + * @param string $name the action name + * @return mixed the result of the action. + * @throws HttpException if the action method does not exist. + */ + public function actionAction($id, $name) + { + $generator = $this->loadGenerator($id); + $method = 'action' . $name; + if (method_exists($generator, $method)) { + return $generator->$method(); + } else { + throw new HttpException(400, "Unknown generator action: $name"); + } + } + public function createUrl($route, $params = array()) { if (!isset($params['id']) && $this->generator !== null) { @@ -99,6 +120,18 @@ class DefaultController extends Controller return parent::createUrl($route, $params); } + public function createActionUrl($name, $params = array()) + { + foreach ($this->module->generators as $id => $generator) { + if ($generator === $this->generator) { + $params['id'] = $id; + break; + } + } + $params['name'] = $name; + return parent::createUrl('action', $params); + } + /** * Loads the generator with the specified ID. * @param string $id the ID of the generator to be loaded.