Commit b77ea4dc by Qiang Xue

Allow generator to define actions.

parent 8c753676
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\gii\controllers; namespace yii\gii\controllers;
use Yii;
use yii\web\Controller; use yii\web\Controller;
use yii\web\HttpException; use yii\web\HttpException;
...@@ -86,6 +87,26 @@ class DefaultController extends Controller ...@@ -86,6 +87,26 @@ class DefaultController extends Controller
throw new HttpException(404, "Code file not found: $file"); 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()) public function createUrl($route, $params = array())
{ {
if (!isset($params['id']) && $this->generator !== null) { if (!isset($params['id']) && $this->generator !== null) {
...@@ -99,6 +120,18 @@ class DefaultController extends Controller ...@@ -99,6 +120,18 @@ class DefaultController extends Controller
return parent::createUrl($route, $params); 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. * Loads the generator with the specified ID.
* @param string $id the ID of the generator to be loaded. * @param string $id the ID of the generator to be loaded.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment