覆盖控制器中的Yii组件

时间:2018-04-17 21:52:48

标签: php yii yii1.x

我在配置文件中加载了Yii 1.x组件,如此

library(dplyr);
library(magrittr);
data %>%
    group_by(branch) %>%
    mutate(sales.norm = sales / sum(sales))    
## A tibble: 21 x 4
## Groups:   branch [3]
#   branch  customer  sales sales.norm
#   <fct>      <dbl>  <dbl>      <dbl>
# 1 Chicago       1. 33816.    0.190
# 2 Chicago       2. 24534.    0.138
# 3 Chicago       3. 47735.    0.269
# 4 Chicago       4.  1467.    0.00826
# 5 Chicago       5. 39389.    0.222
# 6 Chicago       6. 30659.    0.173
# 7 LA            7. 21074.    0.0836
# 8 LA            8. 20195.    0.0801
# 9 LA            9. 45165.    0.179
#10 LA           10. 37606.    0.149    

我需要根据用户在控制器中的位置使 SELECT * into #Temp FROM car INSERT #Temp SELECT a.car_id, c.name, a.color FROM car_alt a INNER JOIN car c ON c.id = a.car_id SELECT * FROM #Temp ORDER BY id, name 属性动态化。这可能吗?如果是这样,我该怎么覆盖呢?

2 个答案:

答案 0 :(得分:0)

可能最好的方法是在Simplesamlphp中扩展init()并配置属性:

class MySimplesamlphp extends Simplesamlphp {

    public $adminAutoloadPath;
    public $nonAdminAutoloadPath;

    public function init() {
        if (Yii::app()->user->isAdmin()) {
            $this->autoloadPath = $this->adminAutoloadPath;
        } else {
            $this->autoloadPath = $this->nonAdminAutoloadPath;
        }

        parent::init();
    }
}

在config中使用新组件:

$config['components']['simplesamlphp'] = array(
    'class' => 'MySimplesamlphp',
    'adminAutoloadPath' => SAML_DIR.'/test2-admin/lib/_autoload.php',
    'nonAdminAutoloadPath' => SAML_DIR.'/test2/lib/_autoload.php',
    'authSource' => 'default-sp',
);

答案 1 :(得分:0)

我认为,即使你没有在配置中初始化它,覆盖yii组件也相当容易。

$component  =   array(
                  'class' => 'application.components.yii-simplesamlphp.components.Simplesamlphp',
                  'autoloadPath' => SAML_DIR.'/'.$tenant_path.'/lib/_autoload.php',
                  'authSource' => 'default-sp',
                    ); //where $tenant_path is the directory of the component i need based on the tenant

    Yii::app()->setComponent('simplesamlphp',$component);

然后使用控制器中的组件

    Yii::app()->simplesamlphp;

注意您只能访问控制器方法中的组件,所以我所做的就是将该代码移动到自己的类中,并在需要创建新实例时调用它成分