Reputation: 1739
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.
Upvotes: 32
Views: 106834
Reputation: 722
I would rather use:
$this->view = 'file';
because any $this->set('var', $val)
you'll have after $this->render('file')
will not reach your view.
In CakePHP 3.x use:
$this->viewBuilder()->template('file');
Deprecated in CakePHP 3.7. Use this instead (as Kuldeep Choudhary suggested in comments)
ViewBuilder::setTemplate('file');
Upvotes: 48
Reputation: 1281
The right way:
$this -> render('TestView/index');
As the answer above mentions, you can use $this -> set
to pass a variable to the View.
However, if that doesn't give you what you want. I'm guessing that you also want the 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 pass data to a non-view file (like .php). CakePHP's views should be ending with .ctp
.
Upvotes: 62
Reputation: 2025
class StocksRealtimeController extends AppController {
var $uses = 'StockRealtime';
function index(){
$this->layout = NULL;
$this->autoRender = false;
$this->set('stocksRT', $this->StockRealtime->find('all'));
$this -> render(`/TestView/index`);
}
}
Upvotes: 0
Reputation: 11
public function admin_index() {
$this->layout = 'admin/table';
$action = '/Vendors';
$this->Prg->commonProcess('Vendor');
$this->paginate = array('conditions' => array($this->Vendor->parseCriteria($this->passedArgs)), 'order' => 'Vendor.created_on DESC', 'limit' => 15);
$this->set('vendor', $this->paginate('Vendor'));
$this->render('/vendors/admin_items');
}
Upvotes: 0
Reputation: 7930
$this->view = '/TestView/index';
$this->set('stocksRT', $this->StockRealtime->find('all'));
Upvotes: 1
Reputation: 1504
class StocksRealtimeController extends AppController
{
var $uses = 'StockRealtime';
function index( )
{
$this->layout = NULL;
$this->autoRender = false;
$this->set('stocksRT', $this->StockRealtime->find('all'));
return $this -> render('/TestView/index');
/*
$this -> render('/TestView/index');
Here 'TestView' must be a Folder named same as "public $name" variable value
in Controller and an "index.ctp" must be situated under TestView Folder.
'index'
*/
}
}
Give it a try, return 'KEYWORD' must be there to render view page successfully. Sorry about 2nd question as i didn't get it. According to CakePHP, variable [stocksTR] which is set using $this -> set( ) , also will be available at manually render view page [ 'index.ctp' ].
Upvotes: 3
Reputation:
Try to put the name of the view without .ctp extension.
$this->render('file');
Upvotes: 11