public createAction ( string $id ) : |
||
$id | string | the action ID. |
리턴 | the newly created action instance. Null if the ID doesn't resolve into any action. |
public function createAction($id)
{
if ($id === '') {
$id = $this->defaultAction;
}
$actionMap = $this->actions();
if (isset($actionMap[$id])) {
return Yii::createObject($actionMap[$id], [$id, $this]);
} elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
$methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
if (method_exists($this, $methodName)) {
$method = new \ReflectionMethod($this, $methodName);
if ($method->isPublic() && $method->getName() === $methodName) {
return new InlineAction($id, $this, $methodName);
}
}
}
return null;
}
/** * @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; }