将控制器渲染到CakePHP中的不同视图 [英] Rendering controller to a different view in CakePHP

查看:179
本文介绍了将控制器渲染到CakePHP中的不同视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法来渲染控制器到不同的视图,然后正常吗?我试图将一些数据从控制器传递到非默认视图。意思是我的控制器被调用:

  class StocksRealtimeController extends AppController {
var $ uses ='StockRealtime'
function index(){
$ action ='/ TestView';
$ this-> set('stocksRT',$ this-> StockRealtime-> find('all'));
// $ this - > viewPath ='Pages';
$ this - > render('/ TestView / index');
}
}

...和我的视图在views->另一个问题是,如何将该值传递给PHP而不是CakePHP框架外的ctp文件?



我已尝试过这里的一切没有运气。

解决方案

正确的方式:



$ this - >正如上面提到的答案,你可以使用 $ this->($ test $ / index'); ; set 将传递变量传递给View。



但是,如果这不会给你想要的,我猜你还想要显示另一个布局(非默认布局)。你可以尝试 $ this - > layout ='layoutname'; (布局在布局文件夹中,默认为default.ctp)



strong> CakePHP的控制器不能够将数据传递到非视图文件(如.php)。 CakePHP的视图应以.ctp


结尾

Is there a way to render a controller to a different view then normal? I'm trying to pass some data from the controller to a non-default view. Meaning my controller is called:

class StocksRealtimeController extends AppController {
    var $uses               = 'StockRealtime';
    function index(){
        $action = '/TestView';
        $this->set('stocksRT', $this->StockRealtime->find('all'));
        //$this -> viewPath = 'Pages';
        $this -> render('/TestView/index');
    }
}

... and My view is in views->TestView->index.ctp

Another question I have is, how to pass that value to a PHP and not a ctp file outside of the CakePHP framework?

I have tried everything from here with no luck.

解决方案

The right way:

$this -> render('TestView/index');

As the answer above mention, you can use $this->set to pass variable to the View.

However, if that doesn't give you what you want I'm guessing that you also want to action to display another layout (non-default layout). You can try doing $this -> layout = 'layoutname'; (Layouts are in the layout folder, default on is default.ctp)

Note: CakePHP's controller isn't designed to be able to pass data to a non-view file (like .php). CakePHP's views should be ending with .ctp

这篇关于将控制器渲染到CakePHP中的不同视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆