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 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案