Page 1 of 1

Zend action issues

Posted: Fri Dec 04, 2009 12:15 pm
by alex.barylski
I have an action controller derived simply from Zend_Controller_Action, looks like this:

Code: Select all

 class IndexController extends Zend_Controller_Action{
 
    public function indexAction()
    {
      //exit;
      //return;
      $this->response->setBody('Output from the action');
    }
 
  }
My action is being invoked as expected, but if I don't use 'exit' as a last command I get an exception:
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php:242
Stack trace:
#0 C:\wamp\www\admin\core\libraries\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 C:\wamp\www\admin\index.php(24): Zend_Controller_Front->dispatch()
#2 {main}
thrown in C:\wamp\www\admin\core\libraries\Zend\Controller\Dispatcher\Standard.php on line 242
Why is this happening, what am I missing in understanding how Zend controllers work?

EDIT | Why would an exception be thrown?? I don't get it I echo'ed $controller from within Dispatcher:

Code: Select all

 
if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
    echo $controller;
        require_once 'Zend/Controller/Dispatcher/Exception.php';
        throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
}
The echo is returning 'error' which is obviously not what is supposed to return. My bootstrap code looks like:

Code: Select all

 
  $front = Zend_Controller_Front::getInstance();
 
  $front->registerPlugin(new Zecko_Controller_Plugin_CheckAuth());
  $front->registerPlugin(new Zecko_Controller_Plugin_NoCache());
 
  $front->setParam('noErrorHandler', true);
  //$front->setParam('noViewRenderer', true);
  //$front->setParam('useDefaultControllerAlways', true);
 
  //$front->setDefaultControllerName('Index');
  $front->setDefaultAction('index');
  $front->setDefaultModule('domain');
 
  $front->setModuleControllerDirectoryName('controllers');
  $front->addModuleDirectory('../../module/system');
 
  $front->dispatch();
 
Paths all seem to be resolving now so why would the dispatcher not find the action, yet when I enter a URL like:

domain/index/test
domain/index/index

So long as I 'exit' at the end of the actions they output as expected, otherwise Zend s***'s all over me??? Obviously the dispatcher/router/whatever is capable of finding the controller:action as the methods are invoked and only fail in the case exit isn't used with the method.


Cheers,
Alex

Re: Zend action issues

Posted: Fri Dec 04, 2009 2:10 pm
by John Cartwright
Sounds like you don't have the necessary view file to render, and furthermore no error controller (which I believe Zend auto dispatches on serious errors/exceptions). I've always included an "ErrorController" since the early days of Zend, so I really never thought about it.

Code: Select all

<?php
class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
 
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
 
                $content =<<<EOH
<h1>Error!</h1>
<p>The page you requested was not found.</p>
EOH;
                break;
            default:
                // application error
                $content =<<<EOH
<h1>Error!</h1>
<p>An unexpected error occurred with your request. Please try again later.</p>
EOH;
                break;
        }
 
        // Clear previous content
        $this->getResponse()->clearBody();
 
        $this->view->content = $content;
    }
}

Re: Zend action issues

Posted: Fri Dec 04, 2009 7:00 pm
by alex.barylski
Thats exactly what the issue was. :)

I disabled viewRendering and voila...problem solved. :)

Re: Zend action issues

Posted: Fri Dec 04, 2009 10:16 pm
by Luke
How is this "general discussion" ? :?

Moving to PHP code...

Re: Zend action issues

Posted: Sat Dec 05, 2009 6:24 pm
by alex.barylski
I intended to post under PHP but I was under general at this time I guess. :)