I use the ZF3 skeletion application. I wonder where I am supposed to catch exceptions globally.
Example: Right now, if I access an invalid route (mysite.com/invalid-route), the application reports an uncaught expection and HTTP response code 200
Fatal error: Uncaught Zend\View\Exception\RuntimeException: No RouteMatch instance provided
I would expect the build-in 404 error page to be triggered.
What am I missing? Can someone point me to the right direction?
The exception is logged properly using the following code:
class Module implements ConfigProviderInterface
{
const VERSION = '3.0.3-dev';
public function onBootstrap()
{
$logger = new Logger();
$writer = new Writer\Stream(__DIR__ . '/../../../data/log/error.log');
$logger->addWriter($writer);
// Log PHP errors
Logger::registerErrorHandler($logger, true);
// Log exceptions
Logger::registerExceptionHandler($logger);
}
This is something you can catch using a Listener, triggered on the early event
MvcEvent::EVENT_ROUTE
.I would suggest using a dedicated class & Factory to separate concerns over the usage of the
onBootstrap
function. You'd do this by registering and "activating" a Listener class, like so:You can use just the
InvokableFactory
for this Listener, as there are no special requirements.NOTE: Used an existing class of my own for the above and modified as I think it should work. However, might contain an error or two ;-) Still, should point you in the right direction I think.