I am trying to use $this->_forward()
to go to another action in the same controller but it is not working. My code is...
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->bodyCopy = "<p>Please select a file to upload.</p>";
$form = new forms_UploadForm();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$this->_forward('displayFileContent');
} else {
$form->populate($formData);
}
} else {
$this->view->form = $form;
}
}
public function displayFileContentAction()
{
die("In displayFileContentAction");
}
}
but I am not getting my die message. I get an error saying 'Page not found'
.
Any help would be greatly appreciated as I have been having real trouble with this.
Why are you forwarding ? just execute the code, replace
$this->_forward('displayFileContent');
with
return $this->displayFileContentAction();
Why dont you try
$this->_redirect('/Controller/displayFileContent');
//Your action
public function displayFileContentAction(){
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
die("In displayFileContentAction");
}
When you call the $this->_forward('displayFileContent')
you go through the routing stack of Zend Framework. The routing then search for the displayfilecontent
action which doesn't exists (url are case-insensitive).
To reach that URL you will need to forward to $this->_forward('display-file-content')
, using dashes instead of camel case.
You will then need to create a view file named display-file-content.phtml
or to disable view rendering for this action
Your code should be:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->bodyCopy = "<p>Please select a file to upload.</p>";
$form = new forms_UploadForm();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
return $this->_forward('display-file-content');
} else {
$form->populate($formData);
}
} else {
$this->view->form = $form;
}
}
public function displayFileContentAction()
{
die("In displayFileContentAction");
}
}
Notes:
$this->_forward('display-file-content')
. Use dashes instead of camel cased.return
to $this->_forward('display-file-content')
or your current Action will continue to execute before forwarding to the other Action.display-file-content.phtml
or just disable the view for this Action using $this->_helper->viewRenderer->setNoRender(true); $this->_helper->layout->disableLayout();
.