devmonster devmonster - 1 year ago 187
PHP Question

Rendering controller to a different view in CakePHP

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.

Answer Source

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