Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';无法使用对象中的方法获取细枝_Php_Templates_Twig - Fatal编程技术网

Php Can';无法使用对象中的方法获取细枝

Php Can';无法使用对象中的方法获取细枝,php,templates,twig,Php,Templates,Twig,我无法让它工作,我需要通过会话对象中的对象检索CSRF令牌,但无论我尝试什么,它都不会显示 我开发了一个系统,可以根据对象的属性生成模板,如下所示: public function render(){ if(method_exists($this->controller, $this->action)){ //We need to check if the method is used in the controller and not in the pare

我无法让它工作,我需要通过会话对象中的对象检索CSRF令牌,但无论我尝试什么,它都不会显示

我开发了一个系统,可以根据对象的属性生成模板,如下所示:

public function render(){
    if(method_exists($this->controller, $this->action)){
        //We need to check if the method is used in the controller and not in the parents AppController and/or Controller
        $f = new ReflectionClass($this->controller);
        $methods = array();
        foreach($f->getMethods() as $m){
            if($m->name == $this->action){
                if($m->class == $this->controller){
                    $this->controller->{$this->action}();
                }
            }
        }
        $this->view_body = $this->render_view();
    }else{
        $this->error->show('Method ' . $this->action . ' not found in  ' . $this->controller, $this->action, $this->controller);
    }
    $this->_set_render_view();
    $this->_set_controller_layout();
    if(!file_exists($this->config->paths->root . $this->layouts_path."/".$this->controller->layout)){
        $this->error->show('Layout not found', $this->action, $this->controller);
    }
    if(!file_exists($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view)){
        $this->error->show('View not found', $this->action, $this->controller);
    }
    $layout_content = file_get_contents($this->config->paths->root . $this->layouts_path."/".$this->controller->layout);
    $view_content = file_get_contents($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view);
    $bind_content = "{% extends \"layout.tmpl\" %} ";
    $templates = array();
    $bind_content .= '{% block view %}' . $view_content . " {% endblock %}";
    $templates['layout.tmpl'] = $layout_content;

    foreach($this->controller->snippets as $snippet_name => $snippet){
        if(!file_exists($this->config->paths->root.$this->snippets_path ."/" . $snippet)){
            $this->error->show('Snippet not found', $this->action, $this->controller);
        }
        $snippet_content = file_get_contents($this->config->paths->root.$this->snippets_path ."/" . $snippet);
        $bind_content .= "{% block ". $snippet_name . " %}" . $snippet_content . " {% endblock %}";
    }   
    $templates[$this->controller->view] = $bind_content;
    $loader = new Twig_Loader_Array($templates);
    $processwire_vars = array('user', 'pages', 'page', 'sanitizer', 'files', 'input', 'permissions', 'roles', 'templates', 'session', 'config', 'controller', 'wire');

    foreach($this->controller->vars as $key=>$val){
        $vars[$key] = $val;
    }

    $twig = new Twig_Environment($loader);
    $twig->addGlobal('this', $this);
    foreach($processwire_vars as $key => $pw_v){
        if(!isset($vars[$pw_v])){
            //$vars[$pw_v] = $this->$pw_v;
            $twig->addGlobal($pw_v, $pw_v);
        }
    }
    echo $twig->render($this->controller->view, $vars);
}
现在我想我可以通过以下操作访问会话的属性:

{{ session.CSRF.getTokenValue() }}

但什么都没有表现出来

当我使用
时,它起作用了,所以它不是失败的对象/方法

我做错了什么

编辑:


还值得注意的是,可以访问user(user.title,user.name)等属性

我认为这可能会对您有所帮助:代码中提到的会话是一个对象,而不是$\u会话。它是框架Processwire()中的一个对象,try
{{{session.CSRF.tokenValue}}
因为twig构建了正确的方法名来调用它。
{{ session['CSRF'].getTokenValue() }}