yii\base\Module::createControllerByID PHP Метод

createControllerByID() публичный Метод

The controller ID is relative to this module. The controller class should be namespaced under [[controllerNamespace]]. Note that this method does not check [[modules]] or [[controllerMap]].
public createControllerByID ( string $id ) : Controller
$id string the controller ID.
Результат Controller the newly created controller instance, or `null` if the controller ID is invalid.
    public function createControllerByID($id)
    {
        $pos = strrpos($id, '/');
        if ($pos === false) {
            $prefix = '';
            $className = $id;
        } else {
            $prefix = substr($id, 0, $pos + 1);
            $className = substr($id, $pos + 1);
        }
        if (!preg_match('%^[a-z][a-z0-9\\-_]*$%', $className)) {
            return null;
        }
        if ($prefix !== '' && !preg_match('%^[a-z0-9_/]+$%i', $prefix)) {
            return null;
        }
        $className = str_replace(' ', '', ucwords(str_replace('-', ' ', $className))) . 'Controller';
        $className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
        if (strpos($className, '-') !== false || !class_exists($className)) {
            return null;
        }
        if (is_subclass_of($className, 'yii\\base\\Controller')) {
            $controller = Yii::createObject($className, [$id, $this]);
            return get_class($controller) === $className ? $controller : null;
        } elseif (YII_DEBUG) {
            throw new InvalidConfigException("Controller class must extend from \\yii\\base\\Controller.");
        } else {
            return null;
        }
    }

Usage Example

Пример #1
0
 public function createControllerByID($id)
 {
     if ($id == 'security' && !$this->enableSecurityHandler) {
         return null;
     } else {
         return parent::createControllerByID($id);
     }
 }