0
我有兩個控制器InvoicesController.php和ProductsController.php。我希望在提交表單invoice_add.ctp時它應該重定向,但不要在step1中保存數據。數據保存在步驟= 2完成,product_add.ctp的網址爲products/product_add?step = 2。我的代碼如下。其不是重定向在cakephp中設置標題重定向
我的git的URL https://github.com/vixy410/HeaderRedirect
InvoicesController.php
<?php
App::uses('AppController', 'Controller');
App::uses('ProductController', 'Controller');
class InvoicesController extends AppController {
public $components = array('Paginator');
public function beforeRender() {
parent::beforeRender();
$step = 1;
$this->set(compact('step'));
$step = $this->request->param('step');
$this->invoice_add();
}
public function invoice_add(){
if($this->request->data('step1')){
$this->response->header(array(
'Location'=>array(
'controller'=>'products',
'action'=>'product_add',
'?'=>array(
'step'=>'2'
)
)
));
}
else{
}
}
//Element/step1.ctp
<div class="invoices form">
<?php echo $this->Form->create('Invoice',array(
'url'=>array('controller'=>'invoices','action'=>'invoice_add')
)); ?>
<fieldset>
<legend><?php echo __('Add Invoice'); ?></legend>
<?php
echo $this->Form->input('client_name');
echo $this->Form->input('client_email');
echo $this->Form->input('phone');
echo $this->Form->submit('Next Step',array('name'=>'data[Invoice][step1]'));
?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Invoices'), array('action' => 'index')); ?></li>
</ul>
</div>
//元/ step2.ctp
<div class="products form">
<?php echo $this->Form->create('Product',array(
'url'=>array('controller'=>'product','action'=>'product_add')
)); ?>
<fieldset>
<legend><?php echo __('Add Product'); ?></legend>
<?php
echo $this->Form->input('invoice');
echo $this->Form->input('title');
echo $this->Form->input('description');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Products'), array('action' => 'index')); ?></li>
</ul>
</div>
//invoice_add.ctp在查看/發票
<div class="container">
<?php
if($step == 1){
echo $this->element('step1');
}
if($step == 2){
echo $this->element('step2');
}
?>
</div>
我應該用 $ this-> response-> send()來代替$ this-> response-> header() – Vikky