Class luya\base\ModuleReflection

Inheritanceluya\base\ModuleReflection » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version1.0.0
Source Code https://github.com/luyadev/luya/blob/master/core/base/ModuleReflection.php

Run any route inside the provided module.

In order to run a module reflection route You have to instantiate the module via the Yii::createObject method

$reflection = Yii::createObject(['class' => ModuleReflection::className(), 'module' => $module]);

Now you can pass optional parameters before the run process, for example to define what controller action you may run:

$reflection->defaultRoute("my-controller", "the-action", ['param1' => 'foo']);

Now in order to return the content of the modules route execute the run method:

$content = $reflection->run();

Public Properties

Hide inherited properties

Property Type Description Defined By
$controller yii\base\Controller|null The controller paramter is null until the run() method has been applied. luya\base\ModuleReflection
$module luya\base\Module The module where the route should be run from. luya\base\ModuleReflection
$request luya\web\Request Request object from DI-Container. luya\base\ModuleReflection
$requestRoute array An array with + route: The path/route to the controller + args: If the url has no params, it returns all params from get request. luya\base\ModuleReflection
$suffix string The suffix which should be used to attach to the url rules. luya\base\ModuleReflection
$urlManager luya\web\UrlManager UrlManager object from DI-Container. luya\base\ModuleReflection
$urlRule array luya\base\ModuleReflection

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Class constructor in order to consum from DI Container. luya\base\ModuleReflection
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
defaultRoute() Inject a defaultRoute. luya\base\ModuleReflection
getModule() Getter for the module property. luya\base\ModuleReflection
getRequestRoute() Determine the default route based on current defaultRoutes or parsedRequested by the UrlManager. luya\base\ModuleReflection
getSuffix() Getter for the suffix property. luya\base\ModuleReflection
getUrlRule() Returns the url rule parameters which are taken from the requested route. luya\base\ModuleReflection
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. luya\base\ModuleReflection
run() Run the route based on the values. luya\base\ModuleReflection
setModule() Setter for the module property. luya\base\ModuleReflection
setRequestRoute() Setter method for the requested route luya\base\ModuleReflection
setSuffix() Setter for the suffix property. luya\base\ModuleReflection

Property Details

Hide inherited properties

$controller public property

The controller paramter is null until the run() method has been applied.

$module public property

The module where the route should be run from.

public luya\base\Module $module null
$request public property

Request object from DI-Container.

public luya\web\Request $request null
$requestRoute public property

An array with

  • route: The path/route to the controller
  • args: If the url has no params, it returns all params from get request.
  • originalArgs: The arguments (params) parsed from the url trogh url manager
public array getRequestRoute ( )
public void setRequestRoute ( $route, array $args = [] )
$suffix public property

The suffix which should be used to attach to the url rules.

public string $suffix null
$urlManager public property

UrlManager object from DI-Container.

$urlRule public read-only property
public array getUrlRule ( )

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Class constructor in order to consum from DI Container.

public void __construct ( luya\web\Request $request, luya\web\UrlManager $urlManager, array $config = [] )
$request luya\web\Request
$urlManager luya\web\UrlManager
$config array

                public function __construct(Request $request, UrlManager $urlManager, array $config = [])
{
    $this->request = $request;
    $this->urlManager = $urlManager;
    parent::__construct($config);
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
defaultRoute() public method

Inject a defaultRoute.

public void defaultRoute ( $controller, $action null, array $args = [] )
$controller string
$action string
$args array

                public function defaultRoute($controller, $action = null, array $args = [])
{
    $this->_defaultRoute = [
        'route' => implode('/', [$this->module->id, $controller, (empty($action)) ? 'index' : $action]),
        'args' => $args,
        'originalArgs' => $args,
    ];
}

            
getModule() public method

Getter for the module property.

public luya\base\Module getModule ( )

                public function getModule()
{
    return $this->_module;
}

            
getRequestRoute() public method

Determine the default route based on current defaultRoutes or parsedRequested by the UrlManager.

See also \luya\base\Related problems and changes: + https://github.com/luyadev/luya/issues/1885 + https://github.com/luyadev/luya/issues/1267 + https://github.com/luyadev/luya/issues/754.

public array getRequestRoute ( )
return array

An array with

  • route: The path/route to the controller
  • args: If the url has no params, it returns all params from get request.
  • originalArgs: The arguments (params) parsed from the url trogh url manager

                public function getRequestRoute()
{
    if ($this->_requestRoute !== null) {
        return $this->_requestRoute;
    }
    
    if ($this->_defaultRoute !== null && empty($this->getSuffix())) {
        $array = $this->_defaultRoute;
    } else {
        // parse request against urlManager
        $route = $this->urlManager->parseRequest($this->request);
        // return human readable array
        $array = [
            'route' => $route[0],
            'args' => $route[1],
            'originalArgs' => $route[1],
        ];
    }
    // resolve the current route by the module
    $array['route'] = $this->module->resolveRoute($array['route']);
    // if there are no arguments, all get params are assigned. In order to use the original arguments from parse request use `originalArgs` instead of `args`.
    if (empty($array['args'])) {
        $array['args'] = $this->request->get();
    }
    
    // @see https://github.com/luyadev/luya/issues/1267
    if ($this->_defaultRoute !== null) {
        $array['args'] = array_merge($this->_defaultRoute['args'], $array['args']);
    }
    $this->_requestRoute = $array;
    
    return $array;
}

            
getSuffix() public method

Getter for the suffix property.

public string getSuffix ( )

                public function getSuffix()
{
    return $this->_suffix;
}

            
getUrlRule() public method

Returns the url rule parameters which are taken from the requested route.

public array getUrlRule ( )

                public function getUrlRule()
{
    $request = $this->getRequestRoute();
    
    return [
        'module' => $this->module->id,
        'route' => $this->module->id . '/' . $request['route'],
        'params' => $request['originalArgs'],
    ];
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
    if ($this->module === null) {
        throw new InvalidConfigException('The module attribute is required and can not be null.');
    }
    
    // add the module specific url rules to the url manager
    $this->urlManager->addRules($this->module->urlRules, true);
}

            
run() public method

Run the route based on the values.

public string|yii\web\Response run ( )
return string|yii\web\Response

The response of the action, can be either a string or an object from response.

throws yii\web\NotFoundHttpException

                public function run()
{
    $requestRoute = $this->getRequestRoute();
    // create controller object
    $controller = $this->module->createController($requestRoute['route']);
    // throw error if the requests request does not returns a valid controller object
    if (!$controller || !isset($controller[0]) || !is_object($controller[0])) {
        throw new NotFoundHttpException(sprintf("Unable to create controller '%s' for module '%s'.", $requestRoute['route'], $this->module->id));
    }
    
    Yii::debug('LUYA module run module "'.$this->module->id.'" route ' . $requestRoute['route'], __METHOD__);
    
    $this->controller = $controller[0];
    $originalController = Yii::$app->controller;
    
    /**
     * Override the current application controller in order to ensure current() url handling which is used
     * for relativ urls/rules.
     *
     * @see https://github.com/luyadev/luya/issues/1730
     */
    $this->controller->on(Controller::EVENT_BEFORE_ACTION, function ($event) {
        Yii::$app->controller = $this->controller;
    });
    /**
     * Restore the original controller instance after rendering.
     *
     * @see https://github.com/luyadev/luya/issues/1768
     */
    $this->controller->on(Controller::EVENT_AFTER_ACTION, function ($event) use ($originalController) {
        Yii::$app->controller = $originalController;
    });
    
    // run the action on the provided controller object
    return $this->controller->runAction($controller[1], $requestRoute['args']);
}

            
setModule() public method

Setter for the module property.

public void setModule ( luya\base\Module $module )
$module luya\base\Module

                public function setModule(Module $module)
{
    $this->_module = $module;
}

            
setRequestRoute() public method

Setter method for the requested route

public void setRequestRoute ( $route, array $args = [] )
$route string
$args array

                public function setRequestRoute($route, array $args = [])
{
    $this->_requestRoute = ['route' => $route, 'args' => $args, 'originalArgs' => $args];
}

            
setSuffix() public method

Setter for the suffix property.

public void setSuffix ( $suffix )
$suffix string

                public function setSuffix($suffix)
{
    $this->_suffix = $suffix;
    $this->request->setPathInfo(implode('/', [$this->module->id, $suffix]));
}