2014-02-28 47 views
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> 

回答

0

有你为什么不使用任何Controller::redirect()具体的原因?

尝试

$this->response->send(); 

设置标头后。但是你的代码只是重新定义了redirect()已经做的事。

+0

我应该用 $ this-> response-> send()来代替$ this-> response-> header() – Vikky