2017-03-08 83 views
0

我正在將我現有的網站從CakePHP 2.x轉換爲CakePHP 3.xCakephp3:訪問控制器內的查看內容

我已經在cakePHP2中使用以下代碼來獲取控制器中的視圖呈現內容。 (我知道這是不是做推薦的方式,但我不知何故需要它。)

ob_start(); 
$view = new View($this, false); 
$payment_form = $view->render(); 
ob_end_clean(); 

我已經試過這但不工作

ob_start(); 
$view = new \Cake\View\View($this->request, $this->response); 
$payment_form = $view->render('/Orders/index'); 
ob_end_clean(); 

我搜索的網頁,但couldn對類似的CakePHP3代碼沒有任何幫助。

回答

2

谷歌搜索和其他試驗/錯誤後,我已想出辦法做下去。

$data = "This will be accessible in view if it will be set in view builder"; 

// create a builder (hint: new ViewBuilder() constructor works too) 
$builder = $this->viewBuilder(); 

// configure as needed 
$builder->autoLayout(false); 
$builder->template('Orders/process_order'); 
$builder->helpers(['Html']); 

// create a view instance (set variables here which you want to access in view) 
$view = $builder->build(['data' => $data]); 

// render to a variable 
$payment_form = $view->render(); 
0

你不需要ob_startob_end_clean,該View處理該部分

$view = new \Cake\View\View($this->request, $this->response); 
$payment_form = $view->render('/Orders/index'); //Returns the view content 

//echo $payment_form; 
+0

這沒有工作親愛的。由於'ob_start/flush'在這裏沒有任何區別。不管怎樣,謝謝! – Parixit