roast_soul
roast_soul

Reputation: 3650

Exception in Controller of Zend Framework

I find a weird thing in Zend Framework 1.12.

In action function, I new a object which doen't exist. Code as below:

 public function signupAction ()
    {

        $tbuser = new mmmm();//mmmm does not exist, so there's exception here
    }

But it doesn't turn to the ErrorController.

I tried below code, it works. It turned to the ErrorController, and showed Application Error.

public function signupAction ()
{
    throw new Exception('pppp');
}

What's wrong? Need I config something else?

Upvotes: 0

Views: 1379

Answers (1)

Tho
Tho

Reputation: 25110

Because "class not found" is falta error, not Exception

So Zend does NOT catch it when calls $controller -> dispatch().

See this block please (Zend_Controller_Dispatcher_Standard):

try {
    $controller->dispatch($action);
} catch (Exception $e) {
    //...
}

To avoid this error, you can use function class_exists to check class has been defined or not before call it.

See this link: class_exists

Update:

By default, falta error will cause current php script to be shut down.

So you need (1)customize error handler and (2)change Falta Error to Exception and it can be catched by ErrorController

Like this (in index.php):

register_shutdown_function('__fatalHandler');

function __fatalHandler() {
    $error = error_get_last();
    if ( $error !== NULL && $error['type'] === E_ERROR ) {
        $frontController = Zend_Controller_Front::getInstance();
        $request = $frontController->getRequest();
        $response = $frontController->getResponse();
        $response->setException(new Exception('Falta error:' . $error['message'],$error['type']));

        ob_clean();// clean response buffer
        // dispatch
        $frontController->dispatch($request, $response);
    }
}

Ref: Zend framework - error page for PHP fatal errors

Upvotes: 2

Related Questions