在cakephp中设置标题重定向

时间:2014-02-28 09:32:51

标签: cakephp

我有两个控制器InvoicesController.php和ProductsController.php。我希望在提交表单invoice_add.ctp时它应该重定向但不在步骤1中保存数据。数据保存在步骤= 2到product_add.ctp完成,其中url products / product_add?step = 2 。我的代码在下面。不重定向

我的git网址 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{

            }
        }

//元素/ 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>

1 个答案:

答案 0 :(得分:0)

您有没有使用Controller::redirect()的具体原因?

尝试

$this->response->send();

设置标题后。但是你的代码只是重新发明了redirect()已有的轮子。