问题
When I click on a submit button i want the page to redirect to the following page?
header('Location: /pdp/policy-info.phtml');
I wrote the above code in the controller code but I am not able to redirect to the above page. it stays on the same page. the filename is called policy-info.phtml in the view.
Also once I redirect, would I be able access my form values through $_POST? Or is there an alternative.
回答1:
ok it sounds to me like you may be missing a few concepts:
You will never redirect to a phtml file. (unless you have written some custom rewrite/route rules) Zend uses the MVC architecture, urls exist in this fashion:
/module/controller/view/key1/value1/keyx/valuex/
generally zend urls don't terminate with file extensions. Also you will never directly call a view file from your browser.In your form tag, you specify where the form submits to with the action attribute. For your url i'm assuming the pdp controller and policy-info action
action="/pdp/policy-info/"
- If you want to redirect after a form submit from with your controller you would use:
$this->_redirect('/pdp/policy-info/');
# maybe you want to execute some code and then execute
# additional code in another controller without re-bootstrapping
$this->_forward('policy-info', 'pdp');
http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.utilmethods
- If you redirect you will not have access to your POST unless you saved those values elsewhere (like in your session). If you forward, I believe the values will still be available in the second action.
回答2:
actually there maybe a few ways to do what you want to do. I haven't tried this first method yet but it should work.
Render a new veiw script from your controller/action if isPost():
public function myAction(){ $form = My_Form(); $this->view->form = $form; //if form is posted and submit = Submit if ($this_request->isPost() && $this_request->getPost()->submit == 'Submit') { if ($form->isValid($this->_request->getPost()) { //this is where you want to capture form data $data = $form->getValues(); //render a new viewscript or _forward to a new action and perform your processing there. $this->render('path to phtml file'); //if user needs to press a button to accept submit = accept ...do some more stuff... } } }I think this or some variation will work.
Note: I don't think _forward resets the request object, so your $_POST data should not be affected.
Also if this policy-info does not require additional input from the user and is just informational you could easily just _forward('action') to a blank action and the router will display the view script.
来源:https://stackoverflow.com/questions/9168061/how-to-redirect-a-page-through-controller-in-zend