dra11767 2012-05-04 08:11
浏览 42
已采纳

Kohana在foo类中使View :: factory('/ foo / bar')命中

My issue is that Kohana only renders the view. When I

View::factory('/foo/bar') in Controller_Other, it doesn't hit Controller_Foo first. I want it to hit the controller, then render the view.

class Controller_Other extends Controller_Template {
    public $template = 'main';
    public function action_index() {
        $this->template->body = View::factory('/foo/bar');
    }
}

How would I get it to run through the controller first:

class Controller_Foo extends Controller_Template {
    public function action_bar() {
        $this->myVar = 'foo';
    }
}

so that in the view, $myVar is always set in views/foo/bar.php when I call it from some other View::factory()?

Edit:

there must be a cleaner way than forcing action_bar to render its own view to string then going:

$foo = new Controller_Foo($this->request, $this->response);
$this->template->body = $foo->action_bar();
  • 写回答

2条回答 默认 最新

  • dougezhua0017 2012-05-04 12:46
    关注

    I'm not sure what you're doing - want to bind global view variable or do an internal request. Anyway, here are the examples for both cases:

    bind global view variable

    class Controller_Other extends Controller_Template {
        public $template = 'main';
    
        public function action_index() {
          View::bind_global('myVar', 'foo'); 
          //binds value by reference, this value will be available in all views. Also you can use View::set_global();
    
          $this->template->body = View::factory('/foo/bar');
        }
    

    }

    do an internal request

    this is 'foo/bar' action

    class Controller_Foo extends Controller_Template {
    
         public function action_bar() {
            $myVar = 'foo';
            $this->template->body = View::factory('/foo/bar', array('myVar' => $myVar);
         }
    }
    
    
    
    class Controller_Other extends Controller_Template {
        public $template = 'main';
        public function action_index() {
             $this->template->body = Request::factory('foo/bar')->execute()->body();
             //by doing this you have called 'foo/bar' action and put all its output to curent requests template body
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?